Hello fellow bakers,
I am having serious problems running find queries on related HABTM
models.
Property hasAndBelongsToMany Market
Market hasAndBelongsToMany Article
Property hasAndBelongsToMany Article
What I am trying to find is the list of articles in the given markets
that the property is associated with. My problem is that every time I
try to run a find query, cake isn't joining the HABTM table. But I
can see in the SQL log that it is using the join table sometimes.
Here is an example
$markets = $this->Property->Market->find('all');
This properly finds all of the Market's and their associated
Properties and Articles. I can tell it is using the join tables from
the SQL log:
12 SELECT `Market`.`id`, `Market`.`title` FROM `cms_markets` AS
`Market` WHERE 1 = 1 27 27 0
13 SELECT `Property`.`id`, `Property`.`client_id`,
`Property`.`metro_id`, `Property`.`title`,
`PropertiesMarket`.`property_id`, `PropertiesMarket`.`market_id` FROM
`cms_properties` AS `Property` JOIN `cms_properties_markets` AS
`PropertiesMarket` ON (`PropertiesMarket`.`market_id` IN (1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27) AND `PropertiesMarket`.`property_id` =
`Property`.`id`) 2 2 0
14 SELECT `Article`.`id`, `Article`.`title`, `Article`.`channel_id`,
`Article`.`metro_id`, `Article`.`content`, `Article`.`author`,
`Article`.`default`, `Article`.`created`, `Article`.`modified`,
`ArticlesMarket`.`article_id`, `ArticlesMarket`.`market_id` FROM
`cms_articles` AS `Article` JOIN `cms_articles_markets` AS
`ArticlesMarket` ON (`ArticlesMarket`.`market_id` IN (1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27) AND `ArticlesMarket`.`article_id` = `Article`.`id`)
But when I try to change the query to give me something more useful it
doesn't work ie:
$markets = $this->Property->Market-
>find('all','conditions'=>array('Property.id'=>$id));
Cake doesn't use the lookup table for this query, resulting in an SQL
error
SQL Error: 1054: Unknown column 'Property.id'
Here is the SQL it generated
SELECT `Market`.`id`, `Market`.`title` FROM `cms_markets` AS
`Market` WHERE `Property`.`id` = 1
Can anyone help me? I must be missing something as nearly all of the
different find calls I have tried do not join with the lookup table.
I am having serious trouble with this.
Thank you very much for any advice or help you may have,
Dave
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 [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