Monday, November 24, 2008

"find" using HABTM relationship

I'm a noob to CakePHP and have an issue with "find" returning results
from tables with a HABTM relationship.

I have 2 tables that have a HABTM relationship:

- Site.id
- Site.title

- User.id
- User.name

The HABTM join table is:
- SiteUser.id
- SiteUser.site_id
- SiteUser.user_id

The Site relationship is defined as follows:
var $hasAndBelongsToMany = array(
'User' =>
array(
'className'=> 'User',
'joinTable'=> 'site_users',
'foreignKey'=> 'site_id',
'associationForeignKey'=> 'user_id',
'unique'=> true
)
);

I am trying to return Sites related to a given User using "find", as
follows:
$options = $this->User->Site->find('list',
array(
'fields'=>array('Site.id', 'Site.title'),
'conditions'=>array('User.id'=>'7')
)
);

However, I get the following error:
Warning (512): SQL Error: 1054: Unknown column 'User.id' in 'where
clause'

The SQL returned shows the HABTM relationship doesn't seem to be
working:
Query: SELECT `Site`.`id`, `Site`.`title` FROM `sites` AS `Site`
WHERE `User`.`id` = 7

Can anyone shed some light on what I'm doing wrong?


Cheers Paul.


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