I would definitely avoid the duplicate data scenario.
> I have a users table (that has user_id, first_name, last_name...etc)
> And a profile table (profile_id, user_id, country)
> And a friends table which has (friend_id, user_id profile_id)
Why does the friends table have profile_id?
If friend_id is pointing to a record in the users table, then there
would be no need to store the profile_id twice.
If friend_id is NOT pointing to a record in the users table, then I
have misunderstood your question and you can ignore the rest of this
post.
If I understand your problem correctly, you need a hasAndBelongsToMany
association which relates the users table to it's self.
Try renaming your friends table to users_friends, and then add this
association to your User model.
var $recursive = 2;
var $hasAndBelongsToMany = array('Friend' =>
array('className'=> 'User',
'joinTable'=> 'users_friends',
'foreignKey' => 'user_id',
'associationForeignKey'=> 'friend_id',
'conditions' => '',
'order'=> '',
'limit'=> '',
'uniq' => true,
'finderQuery' => '',
'deleteQuery' => '',
)
);
if it works correctly (it worked for me when I tested it) you should
find the friend records in the data returned from the User model.
So if your controller code looks like this:
$this->set('user', $this->User->read(null, $id));
Then your view code might look like this:
Friends
hasAndBelongsToMany associations can seem rather complicated if you're
not used to them. If you have trouble getting this code to work, you
should probably read the models chapter of the cake manual, and perhaps
some hasAndBelongsToMany tutorials.
I have seen this question before, but I don't think it was answered, so
please let me know it this works for you.
Good luck,
sc
On Nov 19, 7:23 am, "Mandy" <[EMAIL PROTECTED]> wrote:
> Hi,
>
> This might not necessarily related to cake, but I would like to know
> what's the best way to do this.
>
> I have a users table (that has user_id, first_name, last_name...etc)
> And a profile table (profile_id, user_id, country)
> And a friends table which has (friend_id, user_id profile_id)
>
> Now on the profile page, for that particular profile, I query friends
> table to get the user_id's of all the friends and display their photos,
> because all photos are user_id.jpg, but now I want to display the
> user's first name as well but that is in the user's table.
>
> I have 2 approaches -
>
> 1) Put user_name also in the friends table
> 2) Loop through all friends and for every entry go to the user table to
> get the user first name
>
> What do you guys suggest?
>
> 2nd one will have lots of select queries whereas first one would just
> duplicate data?
>
> Thanks in advance,
> Mandy.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups
"Cake PHP" group.
To post to this group, send email to cake-php@googlegroups.com
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
-~--~~~~--~~--~--~---