I have a previously writen query in other Doctrine project using Query开发者_JAVA技巧 Builder.
$qb->select('c', 'm')
->from('Chapter c')
->leftJoin('c.Book m')
->orderBy('c.chapterno DESC')
->addOrderBy('m.id ASC')
->groupBy('m.id')
->where('c.chapterno = (SELECT MAX(v.chapterno) FROM Chapter v WHERE v.mid = m.id)')
->limit($max);
How can I replicate this in my inherited class from Doctrine_table in symfony?
I am using symfony 1.4
In your ChapterTable
class, something like the following:
public function getChapters($max)
{
$qry = self::getInstance()
->createQuery("c")
->leftJoin('c.Book m')
->orderBy('c.chapterno DESC')
->addOrderBy('m.id ASC')
->groupBy('m.id')
->where('c.chapterno = (SELECT MAX(v.chapterno) FROM Chapter v WHERE v.mid = m.id)')
->limit($max);
return $qry->execute();
}
Then you can call this with eg:
$myMaxValue = 25;
$results = Doctrine::getTable("Chapter")->getChapters($myMaxValue);
Here is a nice solution to organize your queries:
精彩评论