Complex WHERE clauses using the PHP Doctrine ORM

后端 未结 7 1875
长发绾君心
长发绾君心 2020-12-13 09:15

I\'m using the PHP Doctrine ORM to build my queries. However, I can\'t quite seem to figure how to write the following WHERE clause using DQL (Doctrine Query Language):

7条回答
  •  醉梦人生
    2020-12-13 09:54

    The correct way of doing this can be found at doctrine 2 - query builder conditional queries... If statements? as noted by @Jekis. Here is how to use the expression builder to solve this like in @anushr's example.

    $qb->where($qb->expr()->eq('name', ':name'))
      ->andWhere(
        $qb->expr()->orX(
          $qb->expr()->eq('category1', ':category1'),
          $qb->expr()->eq('category2', ':category2'),
          $qb->expr()->eq('category3', ':category3')
      )
      ->andWhere($qb->expr()->lt('price', ':price')
      ->setParameter('name', 'ABC')
      ->setParameter('category1', 'X')
      ->setParameter('category2', 'X')
      ->setParameter('category3', 'X')
      ->setParameter('price', 10);
    

提交回复
热议问题