Re: Problems modeling User - Friend HABTM associations

2007-11-27 Thread Nasko

nate, Sam D: thank you for your replies!

 could be this one
 http://othy.wordpress.com/2006/06/03/unbind-all-associations-except-s...
 Sam D

Yes, I'm using Oth's method mainly in an effort to optimize my
applications as a last phase - to remove unnecessary DB queries. I
didn't feel it was worth mentioning. In this particular case I'm using
it to cut off the rest of the associated models in order to get a
cleaner output arrays.

 Take the associations out of your UserFriend model.  They're implied,
 as UserFriend is already being used to join User and Friend.

Yes, they're implied, but when I remove them I'm not able to fetch
anything beyond UserFriend when issuing $this-User-UserFriend-
findAll():

Outgoing_requests: Array
(
[0] = Array
(
[FanFriend] = Array
(
[id] = 1
[fan_id] = 1
[friend_id] = 2
[requested] = 2007-11-21 16:23:50
[accepted] =
[active] = 0
)
)

[1] = Array
(
[FanFriend] = Array
(
[id] = 5
[fan_id] = 1
[friend_id] = 3
[requested] = 2007-11-26 18:13:59
[accepted] =
[active] = 0
)

)
)

I've been able to find my way through this and these are the steps
I've undertaken to achieve it:

1) As users_friends was violating the principle of alphabetically
ordered foreign keys of the join table, I've renamed the association
model to FanFriend, the corresponding DB table to `fans_friends` and
the foreign key `user_id` to `fan_id`. It still holds semantic meaning
as the user that initiates the friend request could be considered a
fan of the other user, and only after the target user accepts the
request they both become friends

2) I noticed that I've erroneously designated the foreign keys of the
HABTM association. In models/user.php foreign_key and
associationForeignKey should have their places switched.
Was:
var $hasAndBelongsToMany = array(
'Friend' = array(
'className'= 'User',
'joinTable'= 'users_friends',
'foreignKey'   = 'friend_id',
'associationForeignKey'= 'user_id',
'unique'   = true,
'with'   = 'UserFriend',
),
);

Is now:
var $hasAndBelongsToMany = array(
'Friend' = array(
'className'= 'Friend',
'joinTable'= 'fans_friends',
'foreignKey'   = 'fan_id',
'associationForeignKey'= 'friend_id',
'with'   = 'FanFriend',
),
);

3) As can be seen in the latest version of the habtm association, not
only the foreign keys have switched places, but I've also changed the
className - it's not User anymore, but is 'Friend'. Yes, I've found
out that I'm not able to fetch associated model data beyond a model
associated via an alias, no matter what the className was. I've come
to this conclusion by setting up a minimalistic version of my scenario
- User hasAndBelongsToMany Friend (className = 'User') and User
hasMany Photo. No matter how high I set the recursive flag of the
findAll() method, I'm only getting the associated photos on the User
side of the association. The Friend side remained bare - nothing
beyond it.
So, I figured the only way to get anything beyond Friend was to
implement a model for the Friend association and bind the Photo model
to it like this:

models/friend.php:
class Friend extends AppModel {
var $name = 'Friend';
var $useTable = 'users';

var $hasMany = array(
'Photo' = array(
'className' = 'Photo',
'foreignKey'= 'user_id',
'order' = 'Photo.primary DESC, Photo.id ASC',
'dependent' = true,
),

);
}

I first tried to avoid creating a dedicated Friend model, by trying in
users_controller.php:
$this-User-FanFriend-Friend-bindModel('Photo'), but it didn't
work, so as a last resort I implemented the model.

This setup now works and here are the definitions in case they might
help someone:

models/user.php:
class User extends AppModel {
var $name = 'User';

var $hasMany = array(
'Photo' = array(
'className' = 'Photo',
'foreignKey'= 'user_id',
'order' = 'Photo.primary DESC, Photo.id ASC',
'dependent' = true,
),

);

var $hasAndBelongsToMany = array(
'Friend' = array(
'className'= 'Friend',
'joinTable'= 'fans_friends',
'foreignKey'   = 'fan_id',

Re: Problems modeling User - Friend HABTM associations

2007-11-26 Thread Nasko

Could anyone help me out with this one? I'm really stuck :-(
--~--~-~--~~~---~--~~
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: Problems modeling User - Friend HABTM associations

2007-11-26 Thread nate

Take the associations out of your UserFriend model.  They're implied,
as UserFriend is already being used to join User and Friend.

On Nov 26, 3:32 am, Nasko [EMAIL PROTECTED] wrote:
 Could anyone help me out with this one? I'm really stuck :-(
--~--~-~--~~~---~--~~
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: Problems modeling User - Friend HABTM associations

2007-11-26 Thread nate

Also, unless you're using some other code that's not posted here,
there's no method in CakePHP called unbindAll.

On Nov 26, 3:32 am, Nasko [EMAIL PROTECTED] wrote:
 Could anyone help me out with this one? I'm really stuck :-(
--~--~-~--~~~---~--~~
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: Problems modeling User - Friend HABTM associations

2007-11-26 Thread Samuel DeVore

On Nov 26, 2007 8:08 AM, nate [EMAIL PROTECTED] wrote:

 Also, unless you're using some other code that's not posted here,
 there's no method in CakePHP called unbindAll.

could be this one

http://othy.wordpress.com/2006/06/03/unbind-all-associations-except-some/

Sam D


-- 
(the old fart) the advice is free, the lack of crankiness will cost you

- its a fine line between a real question and an idiot

http://blog.samdevore.com/archives/2007/03/05/when-open-source-bugs-me/

--~--~-~--~~~---~--~~
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: Problems modeling User - Friend HABTM associations

2007-11-22 Thread Nasko

Sorry, I forgot to specify:

I'm using CakePHP 1.2 rev. 5693 from the branches.
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---