Php 条令-返回已定义类型的结果
在我的数据库中,我有由字段“type”定义的数据,如: 我想返回我已经定义了这两个常量的“type”字段值的所有结果,而不是所有结果。(类似(NULL)类型) 似乎不支持或运算符 我的代码:Php 条令-返回已定义类型的结果,php,symfony,doctrine-orm,doctrine,symfony-3.4,Php,Symfony,Doctrine Orm,Doctrine,Symfony 3.4,在我的数据库中,我有由字段“type”定义的数据,如: 我想返回我已经定义了这两个常量的“type”字段值的所有结果,而不是所有结果。(类似(NULL)类型) 似乎不支持或运算符 我的代码: return $this->getMyEntityRepository()->findBy([ 'type' => MyEntity::TYPE_ONE || MyEntity::TYPE_TWO ]); ,但在原则中,您应该在存储库中为此类查询创
return $this->getMyEntityRepository()->findBy([
'type' => MyEntity::TYPE_ONE || MyEntity::TYPE_TWO
]);
,但在原则中,您应该在存储库中为此类查询创建一个方法
存储库:
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()
;
}
}
控制器:
return $this->getMyEntityRepository()->findByType();
您还可以为要查找的类型指定参数。,但在原则中,您应该在存储库中为此类查询创建一个方法
存储库:
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()
;
}
}
控制器:
return $this->getMyEntityRepository()->findByType();
还可以为要查找的类型指定参数