Reputation: 8768
I can't find a way to intercept an event when a record is loaded from DB and a model object (usually CActiveRecord
descendant) is filled in with appropriate data - something that is simmetrical to onAfterSave
. The documentation lists the following events: onAfterConstruct
, onAfterDelete
, onAfterFind
, onAfterSave
,
onAfterValidate
, onBeforeDelete
, onBeforeFind
, onBeforeSave
, onBeforeValidate
, onUnsafeAttribute
.
The only thing which could possibly be related is onAfterConstruct
, so I implemented the event handler in my model class derived from CActiveRecord
, but it does not get called.
UPDATE:
In addition to the accepted answer, I have found that there is a protected method instantiate
, which is intended for very same purposes. It can be overridden to access attributes of new instance. Most important that it is called after any instantiation of the record, not only after find
, so it seems to be more reliable.
Upvotes: 1
Views: 5301
Reputation: 1702
In CActiveRecord.php you also have function populateRecord($attributes,$callAfterFind=true) which does a:
//instantiate: by default only returns a new empty object of this class
$record = instantiate()
//then fill all the record attributes
...
//call afterfind
if($callAfterFind) $record->afterFind();
Which means that each time a record is loaded, the afterFind is called. I didn't find other place where the record values get populated so it seems that the afterFind can be interpreted as after load /"afterLoad" (google please index this)
Upvotes: 0
Reputation: 1181
I think what you are looking for is onAfterFind()
, onAfterFind() raised after the record is instantiated, vs onAfterConstruct()
which is raised after the model instance is created by new operator
Upvotes: 4