Saturday, May 29, 2010

Re: HABTM Containable find('all') question

Hi Paul,

Here in (maybe) lies the rub. There isn't an actual admirers table.
There is only a friends table. The reason I am using the admirers/
friends business is to say user A has B friends and C other users have
A as a friend. This was based on AD7six's reply to a 2007 post:
http://groups.google.com/group/cake-php/browse_thread/thread/c8ebc2097f8aad11/fae9ec58501534e6?tvc=2&q=friend#fae9ec58501534e6.

Which is also why I set up this association.

var $hasAndBelongsToMany = array(
'friend' => array('className' => 'User',
'joinTable' => 'Friend',
'foreignKey' => 'user_id',
'associationForeignKey' => 'friend_id'
),
'admirer' => array('className' => 'User',
'joinTable' =>'Friend',
'foreignKey' => 'friend_id',
'associationForeignKey' => 'user_id')
);

I thought I had to do this because when I do a query to list MY
friends - it is possible that I made the connection (my User.id is
Friend.user_id and Friend.friend_id is the other users User.id)

AND

if they make the connection (their User.id is Friend.user_id and
Friend.friend_id is my User.id)

Then I can list both in my views and I make this a little easier for
myself by doing an array_merge of the admirer and friend output of the
original SQL statement.

This is all working amazingly except the final piece which would be
omitting anyone that has been denied by me or has denied me - because
that would just be rubbing salt in the wound:)

I guess the question is this. Can I get a condition from the joined
tables friends and admirers ('Friend.approved !=' => '-1') to happen
within the users conditions? It seems right now that if I put that
condition within the joined tables conditions - the record (of the
denied/denier) is still showing up - however it is leaving out the
friend/admirer merged array. If I take that condition out - then the
record still shows up now with this array:

Array
(
[0] => Array
(
[id] => 4
[created] => 2010-05-03 17:33:54
[Friend] => Array
(
[id] => 39
[user_id] => 4
[friend_id] => 29
[approved] => -1
[message] =>
[created] => 2010-05-22 12:44:05
[modified] => 2010-05-25 13:15:49
)

)

)

I hope this makes sense. I am sorry for all the text - I am just
trying to wrap my head around exactly what I AM trying to do here:)

Thanks again for all of your attention to this!

Brad

On May 29, 2:24 am, WebbedIT <p...@webbedit.co.uk> wrote:
> Do you have access to PhpMyAdmin or something to run queries direct on
> your database?
>
> If so try running this SQL
>
> SELECT DISTINCT User.id, User.username FROM users AS User
> LEFT JOIN admirers AS Admirer ON (User.id = Admirer.user_id AND
> Admirer.approved IN(0,1))
> LEFT JOIN friends AS Friend ON (User.id = Friend.user_id AND
> Friend.approved IN(0,1))
>
> Can you play with the above to see if you get the expected results and
> report back to me?
>
> The idea here is to work out how we would do it without Cake then
> structure our Model::find to produce the same SQL.
>
> HTH
>
> Paul

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

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: