That just makes it throw another error:
SQL Error: 1054: Unknown column 'UserFriend.User.user_id' in 'on clause'
And it's not the HABTM relationship that's causing the problem.
I don't know why, but before saving a HABTM relationship Cake seems to
do a lot of SELECTs for every related model, to make sure it's got the
right users‽
It's tripping over the hasMany relationship between Users and
Accounts, since both have a column with the same name.
On 7 May 2008, at 15:39, Grant Cox (Gmail) wrote:
> Change your HABTM to:
>
> var $hasAndBelongsToMany = array(
> 'Group' => array(
> 'className' => 'Group',
> 'joinTable' => 'groups_users',
> 'foreignKey' => 'user_id',
> 'associationForeignKey' => 'group_id',
> 'unique' => true
> ),
> 'Friend' => array(
> 'className' => 'User',
> 'joinTable' => 'user_friends',
> 'foreignKey' => 'User.user_id',
> 'associationForeignKey' => 'Friend.friend_id',
> 'unique' => true
> )
> );
>
> So you are specifying that in the join of the two tables, 'user_id'
> is the left and 'friend_id' is the right. This should avoid any
> confusion.
>
> Grant
>
>
> David Christopher Zentgraf wrote:
>> Not quite sure what you're getting at, but please have a look:
>>
>>
>> class User extends AppModel {
>>
>> var $name = 'User';
>>
>> var $belongsTo = array(
>> 'DefaultAccount' => array(
>> 'className' => 'Account',
>> 'foreignKey' => 'default_account_id'
>> )
>> );
>>
>> var $hasMany = array(
>> 'Account' => array(
>> 'className' => 'Account',
>> 'foreignKey' => 'user_id',
>> 'dependent' => true
>> )
>> );
>>
>> var $hasAndBelongsToMany = array(
>> 'Group' => array(
>> 'className' => 'Group',
>> 'joinTable' => 'groups_users',
>> 'foreignKey' => 'user_id',
>> 'associationForeignKey' => 'group_id',
>> 'unique' => true
>> ),
>> 'Friend' => array(
>> 'className' => 'User',
>> 'joinTable' => 'user_friends',
>> 'foreignKey' => 'user_id',
>> 'associationForeignKey' => 'friend_id',
>> 'unique' => true
>> )
>> );
>> }
>>
>> On 7 May 2008, at 15:22, Grant Cox wrote:
>>
>>>
>>> Can you post your $hasAndBelongsToMany variable definition? I think
>>> you'll be able to fix your issue by just using the model alias
>>> prefixes in the 'foreignKey' and/or 'associationForeignKey' values.
>>>
>>>
>>> On May 7, 2:25 pm, David Christopher Zentgraf <[EMAIL PROTECTED]>
>>> wrote:
>>>> Hi,
>>>>
>>>> I'm stumbling across bug number 4194.https://trac.cakephp.org/ticket/4194
>>>>
>>>> I have a User model, which has a `name` field.
>>>> This User hasMany Accounts, which also have a `name` field.
>>>> The User also hasAndBelongsToMany other Users as a friend
>>>> relationship.
>>>>
>>>> User (`name`, ...)
>>>> |
>>>> |- hasMany
>>>> | |- Accounts (`name`, ...)
>>>> |
>>>> |- hasAndBelongsToMany
>>>> |- User
>>>>
>>>> When I try to save such a User-User HABTM relationship, Cake is
>>>> doing
>>>> all sorts of lookups, apparently for every linked model.
>>>> What it fails at is this query:
>>>>
>>>> SELECT COUNT(*) AS `count` FROM `users` AS `User`
>>>> LEFT JOIN `accounts` AS `DefaultAccount`
>>>> ON (`User`.`default_account_id` = `DefaultAccount`.`id`)
>>>> WHERE ((`name` = '<username>')) AND `User`.`id` != 1
>>>>
>>>> 1052: Column 'name' in where clause is ambiguous
>>>>
>>>> The WHERE clause would need to be extended to query for
>>>> `User`.`name`
>>>> instead of just `name`, since both tables have a column called
>>>> `name`.
>>>> I'm not quite sure why Cake is doing all these queries in the first
>>>> place, guess it's just being extra paranoid.
>>>>
>>>> Anyway, the above mentioned bug seems to be very related, but nate
>>>> insists on a test case.
>>>> I have no idea how to write test cases for something like this, so
>>>> could a kind soul jump in?
>>>>
>>>> Chrs,
>>>> Dav
>>> >>>
>>
>>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---