编程语言
首页 > 编程语言> > PHP-在Doctrine中有LIMIT的子查询

PHP-在Doctrine中有LIMIT的子查询

作者:互联网

这个问题已经在这里有了答案:            >            Doctrine 2 limit IN subquery                                    1个
我试图做一个查询,该查询具有使用Doctrine的子查询.现在,这给了我一个错误.我在存储库中的功能是:

public function getRecentPlaylists($count = 3) {


    $q = $this->_em->createQuery("
            SELECT p.id,
            p.featuredImage,
            p.title,
            p.slug,         
            a.firstName,
            a.lastName,
            a.slug as authorSlug,
            (SELECT updated 
                     FROM \Entities\Articles 
                     ORDER BY updated DESC LIMIT 1) as updated
            FROM \Entities\Playlist p
            JOIN \Entities\Account a
                        ON p.account_id = a.id
            ")
        ->setMaxResults($count);

            try{    
            return $q->getResult();
            }catch(Exception $e){
                echo $e->message();
            }

}

这给了我这个错误:

[Semantical Error] line 0, col 210 near 'LIMIT 1) as updated FROM': Error: Class 'LIMIT' is not defined.

我几乎放弃了Doctrine,我一直无法弄清楚如何使用子查询或联合查询.这个功能有帮助吗?谢谢!

解决方法:

您需要做的是取出内部查询并为此单独制作DQL,然后在内部使用生成的DQL

$inner_q = $this->_em
    ->createQuery("SELECT AR.updated FROM \Entities\Articles AR ORDER BY AR.updated DESC")
    ->setMaxResults(1)
    ->getDQL();

$q = $this->_em->createQuery("SELECT p.id,
        p.featuredImage,
        p.title,
        p.slug,         
        a.firstName,
        a.lastName,
        a.slug as authorSlug,
        (".$inner_q.") AS updated
        FROM \Entities\Playlist p
        JOIN \Entities\Account a
             ON p.account_id = a.id
    ")
    ->setMaxResults($count);
try{    
    return $q->getResult();
}
catch(Exception $e){
    echo $e->message();
}

标签:doctrine-orm,symfony,zend-framework,mysql,php
来源: https://codeday.me/bug/20191121/2053386.html