Re: Problem with afterFind for associated tables

2012-11-19 Thread Mark Wratten
After experimenting for a while, I discovered that Cake actually calls my 
afterFind function twice for each matching row, but with different data 
formats -

The first time in the form - array(array('Group' => ('id' => ...)))
The second time in the form - array('id' => ... , 'Town' => array('id' => 
...)) so I do get the Town data. It is rather unfortunate that the data 
formats are different as it makes the afterFind code rather ugly.

Mark

On Monday, November 19, 2012 5:37:59 PM UTC-5, Mark Wratten wrote:
>
> We are using Cake 2.2.2 and we have the following tables -
>
> Article -> id, group_id, town_id, ...
> Group -> id, town_id, ...
> Town -> id, ...
>
> The Article and Group tables need to modify their data with data from the 
> Town table, so in each of those Models we have an afterFind callback that 
> checks for the presence of Town data in the results and if found does the 
> updates.
>
> This works fine for the primary model. However Group is also an associated 
> model to the Article table, so if I execute the following find function -
>
> $this->Article->find('all', array('contain' => array('Town', 'Group', 
> 'Group.Town')));
>
> Cake does run the query and assembles the data correctly, but the Group 
> afterFind callback is called without the associated Town data, even though 
> it is part of the query. As far as I can tell, the Group->afterFind is 
> called before the queries for its associated tables are run.
>
> I'm looking for an elegant way to do this within the model, rather than 
> resorting to putting the code in the controller.
>
> Thanks in advance
>
> Mark
>

-- 
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

--- 
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.




Problem with afterFind for associated tables

2012-11-19 Thread Mark Wratten
We are using Cake 2.2.2 and we have the following tables -

Article -> id, group_id, town_id, ...
Group -> id, town_id, ...
Town -> id, ...

The Article and Group tables need to modify their data with data from the 
Town table, so in each of those Models we have an afterFind callback that 
checks for the presence of Town data in the results and if found does the 
updates.

This works fine for the primary model. However Group is also an associated 
model to the Article table, so if I execute the following find function -

$this->Article->find('all', array('contain' => array('Town', 'Group', 
'Group.Town')));

Cake does run the query and assembles the data correctly, but the Group 
afterFind callback is called without the associated Town data, even though 
it is part of the query. As far as I can tell, the Group->afterFind is 
called before the queries for its associated tables are run.

I'm looking for an elegant way to do this within the model, rather than 
resorting to putting the code in the controller.

Thanks in advance

Mark

-- 
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

--- 
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.