本文介绍了Subquery与LIMIT在原则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行一个具有Doctrine的子查询的查询。现在它给我一个错误。我的功能在存储库是:

I'm trying to do a query that has a subquery with Doctrine. Right now it's giving me an error. My function in the repository is:

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();
            }

}

这给我这个错误: / p>

This gives me this error:

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

我几乎放弃了原则,我还没有弄清楚如何做使用子查询或子查询的联合查询。任何帮助这个功能?谢谢!

I'm almost giving up on Doctrine, I haven't been able to figure out how to do queries with subqueries or unions with subqueries. Any help with this function? Thanks!

推荐答案

您需要的是取出内部查询并分开使用DQL,然后使用生成的DQL在

What you need is to take out the inner query and make the DQL separately for that, then use the generated DQL inside

$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();
}

这篇关于Subquery与LIMIT在原则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 01:43