Monday, December 29, 2008

Re: Selecting users who are not friends

On Mon, Dec 29, 2008 at 2:59 PM, WebFeathers <webfeathers@gmail.com> wrote:
>
> Hey-
> I'm trying to generate a list of users who are not friends of the
> currently logged in user.
> I have two tables: profiles, and users_users
> profiles includes the fields: id, first_name, last_name, etc...
> users_users includes: id, user_id, friend_id
>
> I can get the list of users who ARE friends:
> $friends = $this->User->findAll('User.id=' . $target);
>
> ...but can't figure out how to go the other way???
>
> My user model includes:
> class User extends AppModel
> {
> var $name = 'User';
> var $hasAndBelongsToMany = array(
> 'Friends' => array(
> 'className' => 'Profile',
> 'joinTable' =>'users_users',
> 'foreignKey' =>'user_id',
> 'associationForeignKey' => 'friend_id',
> 'unique'=> true
> ),
> 'FriendList' => array(
> 'className' => 'users_users',
> 'joinTable' =>'users_users',
> 'foreignKey' =>'user_id',
> 'associationForeignKey' => 'friend_id',
> 'unique'=> true
> )
> );
> }


I believe if you did this in raw SQL, you would need to do a sub
query. I don't think CakePHPs ORM generates sub queries (subject to
correction).

The simplest solution would be to generate a list of the ids of the
friends, and get all users whose id is not in that list.

[1] show how to do an IN condition

[1] http://book.cakephp.org/view/74/Complex-Find-Conditions

--
Fedora 9 : sulphur is good for the skin
( www.pembo13.com )

--~--~---------~--~----~------------~-------~--~----~
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+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: