从yii CActiveRecord模型中获取属性


Get attributes from yii CActiveRecord model

我有下面这样的模型,其中我定义了一些静态变量(不在DB表中),然后我试图获取这些变量,但它返回了DB表中的那些变量。我正在尝试获取这两个变量(静态变量和DB表中的变量)。

型号

class Eforms extends CActiveRecord
{        
    public $emp_name;
    public $current_status;
    public $action_type;
    public $action_type_extra;
    public $common_value = array(
        1 => 'Yes',
        2 => 'No',
    );
    public $hr_only_value = array(
        1 => 'IT',
        2 => 'BOLD',
    );
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
    public function tableName()
    {
        return 'tbl_eforms';
    }
    public function rules()
    {
        return array(
            array('form_id', 'required'),
            array('form_id, user_id', 'numerical', 'integerOnly'=>true),
            array('name_in_form', 'length', 'max'=>500),
            array('pdf_name', 'length', 'max'=>1000),
            array('emp_name, current_status, action_type, action_type_extra', 'required', 'on'=>'form1'),
            array('emp_name, current_status, action_type, action_type_extra','safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, form_id, user_id, name_in_form, email_recipients, pdf_name, created_on', 'safe', 'on'=>'search'),
        );
    }
    ................
    ...............

控制器:

public function actionIndex()
{
    $model=new Eforms;
     var_dump($model->attributes);exit;
}

如果我用CFormModel更改CActiveRecord,它将返回唯一的静态变量,而不是与DB相关的变量。

来自yii1文档http://www.yiiframework.com/doc/api/1.1/CActiveRecord#attributes-详细

$model->attributes

返回所有列属性值注意,相关对象不是返回

因此,您可以使用访问(相关/计算的)var

 $myVar = $model->emp_name;

 $model->emp_name = 'my_emp_name_value';