简体   繁体   English

Symfony - 在学说查询构建器中使用 orWhere()

[英]Symfony - using orWhere() in doctrine query builder

I want to return query result for the words I am searching in a message but just where userOne or userTwo is currently logged in user.我想返回我在消息中搜索的单词的查询结果,但只是 userOne 或 userTwo 当前登录用户的位置。

I think I have trouble with defining my query builder right because when testing otherwise logged in user is returned correctly.我认为我无法正确定义我的查询构建器,因为在测试时正确返回登录用户。

I am trying with orWhere() clause but it always returns all results of that word not just for logged in user.我正在尝试使用orWhere()子句,但它总是返回该词的所有结果,而不仅仅是登录用户。

My code:我的代码:

 public function search($word, $user)
{
    return $this->getMessageRepository()
        ->createQueryBuilder('a')
        ->where('a.message LIKE :message')
        ->andWhere("a.toUser = $user OR fromUser = $user")
        ->setParameter('message', '%' . $word. '%')
        ->setParameter('toUser', $user)
        ->setParameter('fromUser', $user)
        ->getQuery()
        ->getResult();
}

The logic of the where statement should work as expected. where语句的逻辑应该按预期工作。 But it seems like you are using incorrect parameter binding .但似乎您使用了不正确的parameter binding

toUser and fromUser are columns and therefore no need to bind them. toUserfromUser是列,因此不需要绑定它们。

$user is the target user that we want to filter on, thus it should be bound to the query. $user是我们要过滤的target user ,因此它应该绑定到查询。

An example:一个例子:

{
    return $this->getMessageRepository()
        ->createQueryBuilder('a')
        ->where('a.message LIKE :message')
        ->andWhere("a.toUser = :user OR a.fromUser = :user")
        ->setParameter('message', '%' . $word. '%')
        ->setParameter('user', $user)
        ->getQuery()
        ->getResult();
}

You need to specify all fields with a prefix a because you create this prefix in createQueryBuilder('a') ;您需要使用前缀a指定所有字段,因为您在createQueryBuilder('a')创建了此前缀;

If you have more 1 params use setParameters .如果您有更多 1 个参数,请使用setParameters

And you can OR write in QueryBuilder type -> $builder->expr()->orX .您可以 OR 写入 QueryBuilder type -> $builder->expr()->orX builder- $builder->expr()->orX

Your query example:您的查询示例:

public function search($word, $user)
    {
        $builder = $this->createQueryBuilder('a');
    
        return $builder
            ->where('a.message LIKE :message')
            ->andWhere($builder->expr()->orX(
                $builder->expr()->eq('a.toUser', ':user'),
                $builder->expr()->eq('a.fromUser', ':user'),
            ))
            ->setParameters([
                'message' => '%' . $word . '%',
                'user' => $user,
            ])
            ->getQuery()
            ->getResult();
    }

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

相关问题 在多个 where 查询构建器 Laravel 中使用 orWhere - Using orWhere in a multiple where query builder Laravel 在 Doctrine Symfony2 中使用查询生成器实现 SQL 联合查询 - Implement SQL Union Query using Query Builder in Doctrine Symfony2 如何使用Symfony和Doctrine Query Builder执行连接查询 - How to perform a join query using Symfony and Doctrine Query Builder 如何使用symfony2学说查询构建器选择不同的查询? - How to select distinct query using symfony2 doctrine query builder? Symfony2和Doctrine:使用查询生成器进行多对多关系 - Symfony2 and Doctrine: Many-to-many relation using query builder Laravel 5.4查询构建器在使用orWhere子句时缺少参数2 - Laravel 5.4 query builder Missing argument 2 when using orWhere clause 使用 orWhere 条件时,Laravel 查询构建器无法按预期工作 - Laravel query builder does not work as expected using orWhere conditions Symfony Doctrine 查询生成器 LIKE 在 PostgreSQL 中不起作用 - Symfony Doctrine query builder LIKE not working in PostgreSQL Symfony 2和Doctrine2-在Doctrine查询构建器中使用join时,如何使用列名作为数组键? - Symfony 2 & doctrine2 - how to use column name as array key when using join in doctrine query builder? 如何在 Doctrine 查询构建器 (Symfony) 中使用 countDistinct - How to use countDistinct in Doctrine query builder (Symfony)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM