Symfony 3 / Doctrine findOneBy()使用OR Condition

时间:2017-07-20 07:49:28

标签: symfony doctrine dql

我在Doctrine中遇到了findOneBy()函数的问题。我想在该函数中使用OR条件。这是我的示例代码:

$data = $this->getDoctrineRepo(Employee::class)
    ->findOneBy(['nik' => $code, 'void' => 0]);

这可以实现吗?如果有,请举个例子。感谢。

2 个答案:

答案 0 :(得分:2)

您可以尝试使用 QueryBuilder

$query = $this->createQueryBuilder('u')
    ->select('u')
    ->where('u.nik = :nik OR u.void = :void')
    ->setParameter('nik', $code)
    ->setParameter('void', 0)
;

$result = $query->getQuery()->getOneOrNullResult();

答案 1 :(得分:1)

试试这个;

$data = $this->getDoctrineRepo(Employee::class)
    ->findOneBy([
         '$or' => [
              ['nik' => $code], 
              ['void' => 0]
          ]
     ]
);