从yii CActiveRecord模型中获取属性

时间:2016-05-18 13:16:36

标签: php yii

我有下面的模型,我已经定义了一些静态变量(不在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;
}

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

1 个答案:

答案 0 :(得分:2)

来自yii1 doc http://www.yiiframework.com/doc/api/1.1/CActiveRecord#attributes-detail

$model->attributes
  

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

因此,您可以使用

访问(相关/计算)var
 $myVar = $model->emp_name;

 $model->emp_name = 'my_emp_name_value';