是否可以在yii中将自定义属性添加到CActiveRecord


Is it possible to add custom property to CActiveRecord in yii?

使用后

$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;

您可以像引用表中存在的属性一样引用这些属性。