mbi
(mbi)
1
assume the following tables:
category (id, name)
book (id, categoryId, title)
author (id, firstname, lastname)
book_author (bookId, authorId) //MANY-MANY-Relation between book and author
is it possible to select all authors of a certain category the active record way? Author::model()->findAllByCategory($name)?
Sure!
In Author’s model add this new method method:
public function findAllByCategory($name)
{
$this->getDbCriteria()->mergeWith(array(
'condition'=>'category.name=:categoryName',
'params'=>array(':categoryName'=>$name),
'join'=>'INNER JOIN book_author ON book_author.authorId = t.id INNER JOIN book.id = book_author.bookId INNER JOIN category.id = book.categoryId',
'group' => 't.id'
));
return $this;
}