使用后
$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;
您可以像引用表中存在的属性一样引用这些属性。
关于php - 是否可以在 yii 中向 CActiveRecord 添加自定义属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17293645/