shorif2000
shorif2000

Reputation: 2654

php yii table column name that hass spaces

I am trying to model a class from a table in mssql. The column name has spaces and I cannot remove the spaces or manipulate the column name in any way. I do not know how to incorporate this into yii.

in admin.php i have

    <?php 
print_r($model);
$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'components-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'No_',
        'Description',
        "Original Asset Number",
        'Flag',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

search.php

<?php
/* @var $this FixedAssetController */
/* @var $model FixedAsset */
/* @var $form CActiveForm */
?>

<div class="wide form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'action'=>Yii::app()->createUrl($this->route),
    'method'=>'get',
)); ?>

    <div class="row">
        <?php echo $form->label($model,'No_'); ?>
        <?php echo $form->textField($model,'No_'); ?>
    </div>

    <div class="row">
        <?php echo $form->label($model,'description'); ?>
        <?php echo $form->textField($model,'Description',array('size'=>60,'maxlength'=>255)); ?>
    </div>

    <div class="row">
        <?php echo $form->label($model,'Original Asset Number'); ?>
        <?php echo $form->textField($model,"Original Asset Number",array('size'=>60,'maxlength'=>255)); ?>
    </div>


    <div class="row">
        <?php echo $form->label($model,'flag'); ?>
        <?php echo $form->textField($model,'Flag'); ?>
    </div>

    <div class="row buttons">
        <?php echo CHtml::submitButton('Search'); ?>
    </div>

<?php $this->endWidget(); ?>

</div>

my class FixedAsset.php

<?php

/**
 * This is the model class for table "FixedAsset".
 *
 * The followings are the available columns in table 'FixedAsset':
 * @property string $No_
 * @property string $description
 * @property string $original_asset_number
 * @property int    $flag
 *
 */
class FixedAsset extends AltActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Manufacturers the static model class
     */
    public static function model($className=__CLASS__)
    {
        exit(parent::model($className));
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        //exit("help");
        return "[Spectrum Geo Limited\$Fixed Asset]";
    }

    /**
     * @return array validation rules for model attributes.
     */
    /*public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name', 'required'),
            array('name', 'length', 'max'=>50),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('manufacturer_id, name', 'safe', 'on'=>'search'),
        );
    }*/

    /**
     * @return array relational rules.
     */
    /*public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'components' => array(self::HAS_MANY, 'Components', 'manufacturer'),
        );
    }*/

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        /*return array(
            'manufacturer_id' => 'Manufacturer',
            'name' => 'Name',
        );*/
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        //exit("help");
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('No_',$this->No_);
        $criteria->compare('Original Asset Number',$this->original_asset_number);
        $criteria->compare('Flag',$this->Flag);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }
}

my controller

/**
     * Manages all models.
     */
    public function actionAdmin()
    {

        $model=new FixedAsset('search');
        //exit("help2");
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['FixedAsset']))
            $model->attributes=$_GET['FixedAsset'];

        $this->render('admin',array(
                'model'=>$model,
        ));
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the ID of the model to be loaded
     */
    public function loadModel($id)
    {
        $model=FixedAsset::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }

Error in apache log when tyrying to use Gii

[Tue Sep 18 10:51:22 2012] [error] [client 172.16.0.85] PHP Fatal error:  Maximum execution time of 30 seconds exceeded in /opt/dam/yii-1.1.12.b600af/framework/db/CDbCommand.php on line 497, referer: http://portal-test/dam/index.php?r=gii/model

Upvotes: 0

Views: 1658

Answers (1)

kevin
kevin

Reputation: 11

The column must be specified in the format of "Name:Type:Label", where "Type" and "Label" are optional.

You are getting error because the space is breaking the format.

Just add "Name:" to your columns

Modified version of your code:

 $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'components-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'Name:No_',
        'Name:Description',
        "Name:Original Asset Number",
        'Name:Flag',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

This should now work.

Upvotes: 1

Related Questions