How can I find all records for a model without doing a long list of "OR" conditions?

Posted by gomezuk on Stack Overflow See other posts from Stack Overflow or by gomezuk
Published on 2010-03-08T14:42:30Z Indexed on 2010/03/08 15:36 UTC
Read the original article Hit count: 173

Filed under:
|
|
|

I'm having trouble composing a CakePHP find() which returns the records I'm looking for.

My associations go like this:

User ->(has many)-> Friends ,
User ->(has many)-> Posts

I'm trying to display a list of all a user's friends recent posts, in other words, list every post that was created by a friend of the current user logged in.

The only way I can think of doing this is by putting all the user's friends' user_ids in a big array, and then looping through each one, so that the find() call would look something like:

$posts = $this->Post->find('all',array(
            'conditions' => array(
                'Post.user_id' => array(
                    'OR' => array(
                        $user_id_array[0],$user_id_array[1],$user_id_array[2] # .. etc
                    )
                )           
            )
        ));

I get the impression this isn't the best way of doing things as if that user is popular that's a lot of OR conditions. Can anyone suggest a better alternative?

© Stack Overflow or respective owner

Related posts about cakephp

Related posts about php