Can We Access The Record Array With Only One Attributes In The Model?

New the Model’s attributes is mapped with the fields in the database,

Such as:

Class User extends CModel


     public $username;

     public $email;


$user = User::model()->find();

$user->save(); //even the user has no any change, it will run the save again to database.

Can we do this?

Class User extends CModel


     protected $_attributes = array(





then we use magic method to access the username and email? with this, we can log which attribute has been changed or not. when we run update, we can know this record is changed or not, also we can only know which fields we need to update, no need to update the whole record.

That’s only a very simple example. but I think we don’t access the attribute directly is better, then we can attach more methods when accessing the data.

[color="#006400"]/* Moved from Design Discussions to General Discussion */[/color]

You can do it quite easily using afterFind and beforeSave triggers.

Also you may want to override CActiveRecord::save().