2
使用是否可以在yii中为CActiveRecord添加自定义属性?
$model=Table::model()->findByPk($id);
后,我们可以使用
$p=$model->property;
得到属性值。此属性对应于一个表列,但我希望为$model
添加一个不属于表列的新属性。可能吗?
使用是否可以在yii中为CActiveRecord添加自定义属性?
$model=Table::model()->findByPk($id);
后,我们可以使用
$p=$model->property;
得到属性值。此属性对应于一个表列,但我希望为$model
添加一个不属于表列的新属性。可能吗?
是的,这是可能的。在您的模型中
<?php
/**
* This is the model class for table "{{table}}".
*
* The followings are the available columns in table '{{table}}':
* @property integer $id
.......
*/
class Table extends CActiveRecord
{
部分您可以为您的类定义自定义属性。像这样:
class Table extends CActiveRecord
{
public $aPublicAttribute;
private $_aPivateAttribute;
您可以引用这些就像引用表中存在的属性一样。