ActiveRecord public fields are part of model Attributes ?

I have a model defined below. In my form, other than database columns, i have some extra columns, which i am defining on my own. Things like I agree checkbox, password confirmation etc.

I when i try to copy the attributes like this… they dont get copied.

$model->attributes=$_POST[‘Users’];

when i print out using print_r ($model->attributes)… i see only the table columns not the public variabled defined in that class. Can anybody please help me ?

class Users extends CActiveRecord

{

/**


 * The followings are the available columns in table 'USERS':


 * @var integer $ID


 * @var string $EMAIL


 * @var string $PASSWORD


 * @var string $FIRSTNAME


 * @var string $LASTNAME


 * @var integer $DISABLED


 */


	public $password1;


	public $password2;


	public $password3;

}

thanks

Arvind

For massive assignment to work:

  • in 1.0.x declare them in safeAttributes()

  • in 1.1 use a validation rule.

/Tommy

I have tried to put it inside safe attributes as well. Nothing works. here is the code.

class Users extends CActiveRecord

{

    public $password1;


    public $password2;


    public $password3;





public function safeAttributes()


{


    return array('password1');


}





public function rules()


{


    return array(


    array('EMAIL,FIRSTNAME', 'required'),


        array('EMAIL', 'length', 'max'=>Signup::email_length),


       array('FIRSTNAME', 'length', 'max'=>Signup::firstname_length),


        array('LASTNAME','length','max'=>Signup::lastname_length),





        // email has to be a valid email address


        array('EMAIL', 'email'),


        


        array('password1','required','message'=>'Password cannot be blank.'),


        array('password1','length','max'=>Signup::password_length),








        // passwords must match

array(‘password2’,‘compare’,‘compareAttribute’=>‘password3’,‘message’=>‘New Passwords must match’),

        array('password1','CheckCurrentPassword'),


        array('EMAIL', 'CheckIfUnique'),


    );


}

}

and in the controller. i am doing this to check it out

        $model =$this-> loadUsers(Yii::app()->user->id);


	echo "<pre>";


	print_r($model->attributes);


	echo "</pre><br />";


	if (isset($_POST['Users']))


	{


		$model = new Users;


		echo "<pre>";


		print_r($model->attributes);


		echo "</pre><br />";


             }

The output i am getting is

<pre>

Array

(

[ID] =&gt; 17


[EMAIL] =&gt; arvind@arvind.com


[PASSWORD] =&gt; 70bf9c1f150c32fd4bda1da1ee21b86ff7c58326a6a998dfbdaf0292efcab232


[FIRSTNAME] =&gt; Arvind


[LASTNAME] =&gt; Rangan


[DISABLED] =&gt; 0

)

</pre>

<pre>

Array

(

[ID] =&gt; 


[EMAIL] =&gt; 


[PASSWORD] =&gt; 


[FIRSTNAME] =&gt; 


[LASTNAME] =&gt; 


[DISABLED] =&gt; 

)

</pre>

so for now, i have to asign it manually.

Am i missing something ?

Arvind

P.S. never mind. it suddenly started working by itself.

But still it will not show the declared fields as part of the attributes, when u try to do a print_r($model->attributes).

As far as I concened, the public fields doesn’t appear in attributes list.

But, they can be massively assigned by declaring them at safeAttributes, so they will receive the values.

Also just to add to this, i found that, (this might probably be only in 1.0.x), that, once u declare the safeAttributes() function, then , you have to declare all the attributes which will be mass assigned. Not just the ones, that you think, that are getting omitted.

So for example, if my model has two table columns, say, LOGINID, NAME. and i have public member field in the model, say ADDRESS.

By default, LOGINID and NAME get mass assigned, while ADDRESS wont.

If you want ADDRESS to also get massigned, you have to declare ADDRESS in the safeAttributes() function. Once you declare ADDRESS in the safeAttributes function, then, LOGINID and NAME stop being mass assigned. So you have to include LOGINID and NAME also in the safeAttributes() function.

Hope this might be helpful to some newbie. I spent many hours in discovering this.

thanks

Arvind

You need to include the custom attributes in your model in safeAttributes(). As you’ve noticed, this overrides the parent safeAttributes().

You need to add this to your model and all will work fine:




<?php

public function safeAttributes()

{

  return array_merge(array('custom_field1','custom_field2'), parent::safeAttributes());

}


?>