[ASK]Relational AR: User - UserProfile

Hi,

I am confuse where I have made mistakes, because after I created a model User.php, and UserProfile.php, then use the User model in my controller, and when I print_r the attributes, I got this:

Array


(


    [blocked] => 


    [sys_created_on] => 2009-06-19 07:52:31


    [sys_updated_on] => 0000-00-00 00:00:00


    [username] => 


    [password] => 


    [sys_created_by] => admin


    [sys_updated_by] => admin


    [user_id] => 


)

In my Controller actionSave(), I wrote this line to generate the mentioned above Array result:

$user = new User();


//set attributes


$user->setAttributes($_POST);


$valid = $user->validate('registration');


echo '<pre>';


   print_r($user->attributes);


echo '</pre>';


exit();

The question is, where are the attributes from UserProfile model ?

Thanks before !

My MySQL Tables:

Posted Image Posted Image

My User.php:

class User extends CActiveRecord


{


    public $password_repeat;


    


	/**


	 * Returns the static model of the specified AR class.


	 * @return CActiveRecord the static model class


	 */


	public static function model($className=__CLASS__)


	{


		return parent::model($className);


	}





	/**


	 * @return string the associated database table name


	 */


	public function tableName()


	{


		return 'user_users';


	}





    /**


     * Method to defined rules for validation


     * @return Array


     */


    public function rules()


    {


        return array(


            array('username, password, password_repeat', 'required', 'on' => 'registration'),


            array('username', 'unique', 'on' => 'registration'),


            array('password', 'compare', 'on' => 'registration')


        );


    }





    /**


     * @name relations


     * @return Array


     */


    public function relations()


    {


        return array(


            'userprofile'=>array(self::HAS_ONE, 'UserProfile', 'user_id')


        );


    }





    /**


     * Method to do things before validation occurs


     * @param  <type> $on


     * @return Boolean


     */


    protected function beforeValidate()


    {


        if($this->isNewRecord)


        {


            $this->sys_created_on = date('Y-m-d H:i:s');


            $this->sys_created_by =Yii::app()->user->username;


        }


        else


            $this->sys_updated_on = date('Y-m-d H:i:s');


            $this->sys_updated_by =Yii::app()->user->username;


        return true;


    }


}

My UserProfile.php:

class UserProfile extends CActiveRecord


{


	/**


	 * Returns the static model of the specified AR class.


	 * @return CActiveRecord the static model class


	 */


	public static function model($className=__CLASS__)


	{


		return parent::model($className);


	}





	/**


	 * @return string the associated database table name


	 */


	public function tableName()


	{


		return 'user_profile';


	}





    public function rules()


    {


        return array(


            array('user_id, firstname, lastname, email', 'required'),


            array('email', 'application.extensions.emailsintaxvalidator.EEmailSintaxValidator')


        );


    }





    /**


     * @name relations


     * @return Array


     */


     public function relations()


     {


        return array(


            'user'=>array(self::BELONGS_TO, 'User', 'user_id'),


        );


     }





    /**


     * Method to do things before validation occurs


     * @param  <type> $on


     * @return Boolean


     */


    protected function beforeValidate()


    {


        if($this->isNewRecord)


        {


            $this->sys_created_on = date('Y-m-d H:i:s');


            $this->sys_created_by =Yii::app()->user->username;


        }


        else


            $this->sys_updated_on = date('Y-m-d H:i:s');


            $this->sys_updated_by =Yii::app()->user->username;


        return true;


    }


}

$user->setAttributes($_POST['User']) ?

Quote

$user->setAttributes($_POST['User']) ?

Hi qiang, where is this 'User' refers to ? To the form name ? or to the button name ?

Anyway, I have tried using the name of the submit button:

$user->setAttributes($_POST['btnSave']);

and also using the form name:

$user->setAttributes($_POST['adminForm'];

All results the same, I don't see the attributes of UserProfile model in the User model.

Here is what I got when I print_r ($_POST):

Array


(


    [btnSave] => Save


    [user_id] => 


    [firstname] => 


    [lastname] => 


    [email] => 


    [username] => 


    [password] => 


    [password_repeat] => 


)


Here is what I got when I print_r($user->relations()):

Array


(


    [userprofile] => Array


        (


            [0] => CHasOneRelation


            [1] => UserProfile


            [2] => user_id


        )





)

Here is what I got when I print_r($userProfile->relations()):

Array


(


    [user] => Array


        (


            [0] => CBelongsToRelation


            [1] => User


            [2] => user_id


        )





)


I guess the relation view does not clear enough, since I forgot to expand the INDEXES at the bottom of the view, so here is my SQL codes:

CREATE TABLE IF NOT EXISTS `user_users` (


  `user_id` int(11) unsigned NOT NULL AUTO_INCREMENT,


  `username` varchar(50) NOT NULL,


  `password` varchar(50) NOT NULL,


  `blocked` tinyint(1) NOT NULL DEFAULT '0',


  `sys_created_by` varchar(255) NOT NULL,


  `sys_updated_by` varchar(255) DEFAULT NULL,


  `sys_created_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',


  `sys_updated_on` datetime DEFAULT '0000-00-00 00:00:00',


  PRIMARY KEY (`user_id`),


  UNIQUE KEY `username` (`username`)


) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `user_profile` (


  `user_id` int(11) unsigned NOT NULL DEFAULT '0',


  `firstname` varchar(255) NOT NULL DEFAULT '',


  `lastname` varchar(255) DEFAULT NULL,


  `email` varchar(255) NOT NULL DEFAULT '',


  `sys_created_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',


  `sys_updated_on` datetime DEFAULT '0000-00-00 00:00:00',


  `sys_created_by` varchar(255) NOT NULL DEFAULT '',


  `sys_updated_by` varchar(255) DEFAULT NULL,


  PRIMARY KEY (`user_id`)


) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Thanks!

Since your $_POST is largely empty, what would you expect the attributes be?

Quote

Since your $_POST is largely empty, what would you expect the attributes be?

Well, I was expecting my user.php model:

Array


(


    [blocked] =>


    [sys_created_on] => 2009-06-19 07:52:31


    [sys_updated_on] => 0000-00-00 00:00:00


    [username] =>


    [password] =>


    [sys_created_by] => admin


    [sys_updated_by] => admin


    [user_id] =>


    [firstname] =>   


    [lastname] =>    


    [email] => 


)

Are these attributes supposed to be added to the User.php automaticaly after I defined relations() function ?

[firstname] => 

[lastname] =>   

[email] =>

You are setting sys_created_by in beforeSave which occurs when you call save(). You didn't call this, and that's why.

Relation has nothing to do with firstname, lastname.

Quote

You are setting sys_created_by in beforeSave which occurs when you call save(). You didn't call this, and that's why.

Relation has nothing to do with firstname, lastname.

I see, I guessed that I have misunderstood this line at http://www.yiiframew…e/database.arr:

Quote

The declaration of relationships in an AR class implicitly adds a property to the class for each relationship.

So, this AR Relations will not add property/attributes to User ?

Nope.

I've gained experiences again in Yii today, thank you!