简体   繁体   English

Symfony2 +准则的查询生成器-其中!= 1

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

I have a function that creates a query to my db, like this: 我有一个函数可以创建对数据库的查询,如下所示:

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

My problem is, how to set :type to be not in ? 我的问题是,如何设置:type not in I mean, I wanted to use it like this: 我的意思是,我想这样使用它:

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

But it didn't worked at all. 但这根本没有用。 Using $type = -1 doesn't help either. 使用$type = -1也无济于事。 Is there any way, other than creating if/else statement and duplicating query? 除了创建if / else语句和复制查询外,还有什么办法吗?

Why don't you use a query builder ?? 为什么不使用查询生成器

In that way you can easily customize your query, depending on some condition. 这样,您可以根据某些条件轻松自定义查询。

This is an example: 这是一个例子:

$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');
} 
[...]

Remember to call return $q->getResult(); 记住要调用return $q->getResult(); at the end 在末尾

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM