Jordy,

You could do something like:

1. get all pets .id of a user

$userPets = $this->User->Pet->find('all', array(
        'fields' => array('Pet.user_id'),
        'conditions' => array(
                'Pet.user_id' => $uid
        ),
));

$petIds = Set::combine($userPets, '{n}.Pet.id');

$posts = $this->User->Pet->Post->find('all', array(
        'conditions' => array(
                'Postt.pet_id' => $petIds
        ),
        'order' = array('Post.created' = 'DESC')
));

This is just a basic idea to do it with 2 mysql queries..
Also start from the post leg since thats the main thing what you need,
this queries should be in the posts controller…
You can add paginate, contain etc to it...

Andras Kende
http://www.kende.com


On Nov 29, 2011, at 1:13 PM, Jordy van Kuijk wrote:

> Hi all,
> 
> I've managed to set up most of my associations and models in CakePHP (I'm a 
> noobie).
> Now I have a very basic question. Let me explain the situation first.
> I have got a User, Pet and Post model.
> The user hasmany pets, the pet hasmany posts.
> The user can also log in. 
> 
> Now in my controller, I want to access the Posts of the pets of the current 
> user.
> To do this, I'm using the following method, which I think is not really what 
> I'm looking for:
> 
> #in user.php (model)
> 
> public function getPosts($uid = null){
> $user = $this->findById($uid);
> $pets = $user['Pet'];
> $result = array();
> foreach ($pets as $pet){
> $pett = $this->Pet->findById($pet['id']);
> $result = array_merge($result, $pett['MyFeed']);
> }
> return $result;
> }
> 
> #in my controller
> 
> $feed = $this->User->getPosts($this->Auth->user('id'));
> 
> Not only can i not order my posts correctly (by date), but I also think this 
> is not the way to go.
> Do you have any suggestions on doing this better? Isn't it possible to store 
> the logged in User somewhere so that accessing it's associations is easier? 
> 
> Thanks in advance,
> 
> Jordy
> 
> 
> 
> -- 
> Our newest site for the community: CakePHP Video Tutorials 
> http://tv.cakephp.org 
> Check out the new CakePHP Questions site http://ask.cakephp.org and help 
> others with their CakePHP related questions.
>  
>  
> To unsubscribe from this group, send email to
> [email protected] For more options, visit this group at 
> http://groups.google.com/group/cake-php

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to