簡體   English   中英

Symfony2 +准則的查詢生成器-其中!= 1

[英]Symfony2 + doctrine's query builder - where != 1

我有一個函數可以創建對數據庫的查詢,如下所示:

public function getList($u, $t, $ls, $lf) {
        return $this->getEntityManager()
            ->createQuery('
                    SELECT
                        o,
                        u,
                        g,
                        r,
                        t,
                        p
                    FROM GameShelfUsersBundle:Own o
                    LEFT JOIN o.user u
                    LEFT JOIN o.game g
                    LEFT JOIN o.rate r
                    LEFT JOIN o.typo t
                    LEFT JOIN o.platforms p
                    WHERE u.id = :user
                    AND o.typo = :type
                    ORDER BY o.updated DESC
                ')
            ->setParameters(array(
            'user' => $u,
            'type' => $t
            ))
            ->setMaxResults($lf)
            ->setFirstResult($ls)
            ->getResult();
    }

我的問題是,如何設置:type not in 我的意思是,我想這樣使用它:

$type = '!= 1'
...
AND o.typo :type
...
'type' => $type

但這根本沒有用。 使用$type = -1也無濟於事。 除了創建if / else語句和復制查詢外,還有什么辦法嗎?

為什么不使用查詢生成器

這樣,您可以根據某些條件輕松自定義查詢。

這是一個例子:

$q = $this
        ->createQueryBuilder('foo')
        ->select('foo')
        ->leftJoin('foo.bar', 'foobar')
        ->leftJoin('foobar.bar', 'foobarbar')
        ;
if($myVar > 0)
{
 $q->where('foobarbar.var = :myVar');
}
else
{
 $q->where('foobarbar.var = :staticValue');
} 
[...]

記住要調用return $q->getResult(); 在末尾

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM