I have a Message
model that hasMany Librarian
.
The problem I am having is that when I try to join a table to the Librarian
table, that table has not been joined yet - i.e. the join I create appears before the relationship join is created.
$this->Message->find('all', array(
'joins' => array(
array(
'table' => 'users',
'alias' => 'User',
'conditions' => array('User.id = Librarian.id')
)
)
));
This generates a query along these lines:
SELECT `Message`.`id`, `Message`.`librarian_id`,
`Message`.`Librarian`.`id`, `Librarian`.`user_id`
FROM `contact_messages` AS `Message`
INNER JOIN users AS `User` ON (`User`.`id` = `Librarian`.`user_id`)
LEFT JOIN `librarians` AS `Librarian`
ON (`Message`.`librarian_id` = `Librarian`.`id`)
WHERE `Message`.`id` = 3
I get the error
Unknown column 'Librarian.user_id' in 'on clause'
How can I join to a hasMany table after it has already been included in the build query?
Cheers