Re: Querying Question

2006-11-19 Thread [EMAIL PROTECTED]

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
-~--~~~~--~~--~--~---



RE: Querying Question

2006-11-19 Thread Adrian Godong

>From database normality PoV, you should stick with your current structure
(approach 2). Complexity of SELECT query should belong to CakePHP engine,
not yours.

If you have defined the Model Association correctly, CakePHP will also
retrieve related tables, so you shouldn't worry about how to query.

>From what I understand, you will have the following associations:

User hasOne Profile (and Profile belongsTo User)
User hasMany Friend (and Friend belongsTo User)

If you queried (findAll) a Profile, CakePHP will also fetch related User,
Friends, and Friends' User. CakePHP have a recursive limit of 3, that means
you can only get 3 associated tables from the current one. This is good
enough for your problem.

-Original Message-
From: cake-php@googlegroups.com [mailto:[EMAIL PROTECTED] On Behalf
Of Mandy
Sent: 19 Nopember 2006 19:24
To: Cake PHP
Subject: Querying Question


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
-~--~~~~--~~--~--~---