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:
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;
}
}

