我有下面的模型,我已经定义了一些静态变量(不在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
,它将返回唯一的静态变量,而不是与数据库相关的静态变量。
答案 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';