DS9
DS9

Reputation: 3033

Get attributes from yii CActiveRecord model

I have model like below, where i have defined some static variables (which is not in DB table) then i am trying to fetch those variables but it returns those variables which is in DB table. I am trying to fetch both variables (static variables as well as variables which is in DB table).

Model

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'),
        );
    }

    ................
    ...............

Controller :

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

If i changes CActiveRecord with CFormModel the it returns the only static variables not the DB related one.

Upvotes: 2

Views: 2622

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133370

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

$model->attributes

Returns all column attribute values. Note, related objects are not returned.

So you can access to the (related/calculated) var using

 $myVar = $model->emp_name;

or

 $model->emp_name = 'my_emp_name_value';

Upvotes: 4

Related Questions