When retrieving the favorites, use recursive = 1 to get the associated
models too!
$favorites = $this->User->Favorite->find('all', array(
'conditions' => array('Favorite.user_id' => $id),
'recursive' => '1'
)
);
A better solution for your future development is to look into the
Containable behaviour, which will make your retrieval of associated
model easier. My own experience is exactly that, I also started out
using recursive, but have recently started using the Containable
behavour, and that got a lof of weight off my shoulders!
Containable behaviour "http://book.cakephp.org/view/474/Containable"
Enjoy,
John
On Oct 17, 12:28 am, "Tamim A." <[email protected]> wrote:
> Thanks John,
>
> I went ahead with that, but am using a separate table called favorites to
> store the association. I'm using:
>
> ThingFunction model:
>
> var $belongsTo = array(
> 'User' => array(
> 'className' => 'User',
> ),
> );
>
> var $hasAndBelongsToMany = array(
> 'User' => array(
> 'joinTable' => 'favorites',
> 'unique' => true,
> )
> );
>
> User model:
>
> var $hasMany = array(
> 'ThingFunction' => array(
> 'className' => 'ThingFunction',
> 'conditions' => 'ThingFunction.is_inappropriate = 0'
> )
> );
>
> var $hasAndBelongsToMany = array(
> 'ThingFunction' => array (
> 'joinTable' => 'favorites',
> 'unique' => true,
> 'conditions' => 'ThingFunction.is_inappropriate = 0'
> )
> );
>
> In my Users controller I'm trying to fetch a user's favorites using this:
>
> $favorites = $this->User->Favorite->find('all', array(
> 'conditions' => array('Favorite.user_id' => $id)
> )
> );
>
> When I look at the debug info, I get the following results:
>
> Array
> (
> [0] => Array
> (
> [Favorite] => Array
> (
> [id] => 1
> [user_id] => 1
> [thing_function_id] => 56
> )
>
> )
>
> )
>
> The data above is correct, but I would also like to get the ThingFunction
> title and description (each ThingFunction has fileds thing_function_title
> and thing_function_description). How would I get the ThingFunction details?
>
> Also, I have recursive set to -1 in my AppController.
>
> Thanks much!
>
> Best Regards,
>
> Tamim
>
> On Fri, Oct 16, 2009 at 11:44 AM, John Andersen
> <[email protected]>wrote:
>
>
>
> > You have two associations between the User and the ThingFunction
> > model:
> > - the owner association is - User hasMany ThingFunction -
> > ThingFunction belongsTo User.
> > - the favorites association is - User hasAndBelongsToMany
> > ThingFunction and vice verse.
>
> > Hope this helps you on the way :)
> > Enjoy,
> > John
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---