我正在尝试从mssql中的表建模一个类。列名有空格,我无法以任何方式删除空格或操纵列名。我不知道如何将其纳入yii。
在admin.php我有
<?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>
我的班级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,
));
}
}
我的控制器
/**
* 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;
}
使用Gii进行打字时,apache日志出错
[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
答案 0 :(得分:1)
必须以“名称:类型:标签”的格式指定列,其中“类型”和“标签”是可选的。
您收到错误,因为空格正在破坏格式。
只需在列中添加“名称:”
即可修改后的代码版本:
$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',
),
),
)); ?>
现在应该可以了。