Containable unexpected behavior in related models
We have the Ebooks HABTM Tags. And we try to select the Ebooks that have belong to the tag with id=160.
Both use the containable behavior, so in the Ebook开发者_StackOverflow社区s Controller I have written the following:
$this->Ebook->contain('Tag.id = "160"');
$ebooks = $this->Ebook->find('all');
According the book this should return the needed result. Instead of that, a list of all ebooks is given back.
Note also that two queries are run, the first returns the list of all Ebooks and the second the ebooks that should be returned. Does anyone have any idea?
Thanks in advance
Yes, this query says "Find all Ebooks and include with them all Tags with the id 160".
contain
does not limit the primary result, only the related results.
You need to make an SQL JOIN
to the HATBM table and filter your primary results on it, like so:
$this->Ebook->bindModel(array('hasOne' => array('EbooksTag')));
$this->Ebook->find('all', array(
'conditions' => array('EbooksTag.tag_id' => 160)
));
精彩评论