When I create new User(), related objects are not saved:
User model:
class User extends CActiveRecord
{
public $name;
public $password;
public $loginIp;
public $registrationDate;
public $userStatusId = UserStatus::WaitingApproval;
public $userRoleId = UserRole::User;
/**
* 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';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
return array(
array('name','length','max'=>80),
array('password','length','max'=>32),
array('loginIp','length','max'=>15),
array('password, registrationDate', 'required'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
return array(
'status' => array(self::BELONGS_TO, 'UserStatus', 'userStatusId'),
'role' => array(self::BELONGS_TO, 'UserRole', 'userRoleId'),
'profile' => array(self::HAS_ONE, 'UserProfile', 'userId'),
'emails' => array(self::HAS_MANY, 'UserEmail', 'userId'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
);
}
}
The main problem lies in education. That is, in order to support this feature, users need to learn a lot more. And it often causes more trouble than benefit.
We may add this feature in future if we receive enough requests for it.
I am implementing a custom afterValidate() and afterSave() that considers related objetcs that are set, but I cannot access a private variable in a subclass of CActiveRecord.
abstract class CActiveRecord extends CModel
{
...
private $_related=array(); // attribute name => related objects
...
}
Is it possible and/or correct to change visibility to protected? Only this way I can implement a setRelated() in a subclass.
Is there any other concrete way to implement this? Behaviors are not so well documented, I can’t imagine how would it be.
The work is done with these two methods, afterValidate() and afterSave(), changing the visibility of variable above and implementing a custom setAttribute().