drgwsx8405
2019-07-04 09:58
浏览 49
已采纳

Doctrine - 返回定义类型的结果

In my db I have data that I've defined by field "type" like:

const TYPE_ONE = 'one';
const TYPE_TYPE = 'two';

I want to return all results where I have defined value of "type" filed by this two constants and not all of them. ( like (NULL) type).

It seems OR operator is not supported.

My code:

return $this->getMyEntityRepository()->findBy([
            'type' => MyEntity::TYPE_ONE || MyEntity::TYPE_TWO
        ]);

图片转代码服务由CSDN问答提供 功能建议

在我的数据库中,我有数据,我已经按字段“类型”定义,如:

  const TYPE_ONE ='one'; 
 nstst TYPE_TYPE ='two'; 
   
 
 

我想返回我定义的所有结果 由这两个常数提交的“类型”的值而不是全部。 (如(NULL)类型)。

似乎不支持OR运算符。

我的代码:

 返回$ this-> getMyEntityRepository() - > findBy([
'type'=>  ; MyEntity :: TYPE_ONE || MyEntity :: TYPE_TWO 
]); 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doubi2014 2019-07-04 10:14
    已采纳

    OR operator is supported, but in doctrine you should create a method in your repository for this kind of queries.

    Repository:

    class MyEntityRepository extends ServiceEntityRepository
    {
        public function findByType()
        {
            return $this->createQueryBuilder('r')
                ->andWhere('r.type IN (:types)')
                ->setParameter('types', [MyEntity::TYPE_ONE, MyEntity::TYPE_TWO])
                ->getQuery()
                ->getResult()
            ;
        }
    }
    

    Controller:

    return $this->getMyEntityRepository()->findByType();
    

    Also you can specify parameter for types to look for.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题