Namal
Namal

Reputation: 2071

yii model attribute cannot find

I am new to yii. In default actionLogin() method in SiteController, I am searching for where $model->attribtes is defined?

$model->attributes=$_POST['LoginForm'];

I searched in LoginForm, CFormModel, CModel classes but I couldn't find. Is it a setter method?

Upvotes: 0

Views: 545

Answers (3)

fwallen
fwallen

Reputation: 48

The 'attributes' property is really a getter/setter method(s) in the model. Passing an array to it will attempt to do a 'mass' setting of those attributes. All the elements in your $_POST['LoginForm'] need to have a corresponding attribute in the model. In the LoginForm model that comes with Yii, those are set as properties (i.e., public $username;), the attributes property will assign $username with the associated value in the array ('username'=>'myusername').

Upvotes: 0

Namal
Namal

Reputation: 2071

I printed the output of class_parents($model). It says this is inherited from CComponent. So I think $attributes propery is declared in __set() method in CComponent class.

Upvotes: 0

Telvin Nguyen
Telvin Nguyen

Reputation: 3559

http://www.yiiframework.com/doc/guide/1.1/en/form.model#securing-attribute-assignments

After a model instance is created, we often need to populate its attributes with the data submitted by end-users. This can be done conveniently using the following massive assignment:

$model=new LoginForm;
if(isset($_POST['LoginForm']))
    $model->attributes=$_POST['LoginForm'];

The last statement is called massive assignment which assigns every entry in $_POST['LoginForm'] to the corresponding model attribute. It is equivalent to the following assignments:

foreach($_POST['LoginForm'] as $name=>$value)
    {
        if($name is a safe attribute)
            $model->$name=$value;
    }

Yii provides access to lots of other things via an instance variable, such as database fields, relations, event handlers, and the like. These "attributes" are a very powerful part of the Yii framework, and though it's possible to use them without understanding, one can't really use the full power without going under the covers a bit.

attributes is a property of CActiveRecord, and yes, it has getter and setter method

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

Then location where you would like to see

https://github.com/yiisoft/yii/blob/1.1.14/framework/db/ar/CActiveRecord.php#L754

Upvotes: 3

Related Questions