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;
您可以引用這些就像引用表中存在的屬性一樣。