set Model property in afterFind based on related model data gotcha ...

hi all,

the title of the post is a bit cryptic I guess …

here is what I am trying to do and what happens :

I am trying to set a custom model property in afterFind method based on data in a related model :

model 1 : Event

model 2 : Artist

I am calling a lazy find in afterFind to get the data I need.

in Event :

public function afterFind() {


	//add artist ids from related artists objects

	$this->artistsIds = array();//CUSTOM MODEL PROPERTY

	foreach($this->Artist as $artist) {//lazy loading of Artists


		array_push($this->_artistsIds, $artist->id);







so far so good, this works

my problem is that I want to do a similar thing in Artist with Event data :

public function afterFind() {


	if(!empty($this->Event))	{


		$this->eventName =  $this->Event[0]->name;







from here I get a  :

Fatal error: Maximum function nesting level of '100' reached, aborting!

because afterFinds are called repeatedly forever …

so iI was wondering how I could achieve this :

set a custom model property with related model data that also has an afterFind method doing the same with the first model.

I guess I could resort to non AR methods but it would be nice if this was possible and that I just don't know how to do it.

thanks in advance.

You should avoid such recursive usage. It's dangerous and very inefficient.

If you want to build up the artist ID arrays, you can use a more lazy way by declaring a method named getArtistsIds. In this method, you can retrieve the artist IDs when the method is first called.

You should use a different approach by not setting the custom property in the afterFind method. Instead the new method in the model could look like this:

public function getArtistsIds() {

    if(is_null($this->_artistsIds)) {

        $_artistsIds = array();

        foreach($this->Artist as $artist) {

           array_push($this->_artistsIds, $artist->id);



    return $this->_artistsIds;


You can now access this property like this: $event->artistsIds

With a similar function in your artist model you can do the same with eventName.

Another advantage is, that this is now actually lazy, since data is only queried when you access the custom property.

I hope this idea helps.

thank you both

it helps a lot

I'm getting accustomed to the lazy loading and getter/setter

feels really powerful