开发者

Query builder in Doctrine_Table?

开发者 https://www.devze.com 2023-03-19 02:01 出处:网络
I have a previously writen query in other Doctrine project using Query开发者_JAVA技巧 Builder. $qb->select(\'c\', \'m\')

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:

0

精彩评论

暂无评论...
验证码 换一张
取 消