【问题标题】:Doctrine NOT IN queryDoctrine NOT IN 查询
【发布时间】:2016-04-06 17:52:53
【问题描述】:

我有:

$from = new \DateTime($from);
            $to = new \DateTime($to);
            $qb = $this->em->createQueryBuilder();
            $reservations = $qb
                    ->select('rr.id')
                    ->from('\Model\Reservation', 're')
                    ->join('re.rooms', 'rr')
                    ->where('(re.fromDate < ?1 AND re.toDate <= ?2 AND re.toDate > ?1) OR (re.fromDate >= ?1 AND re.fromDate < ?2)')
                    ->setParameters(array(1 => $from, 2 => $to))
                    ->getQuery()
                    ->getResult();

我有结果:

array(2) { [0]=> array(1) { ["id"]=> int(2) } [1]=> array(1) { ["id"]=> int(2) } }

我想用 SELECT ... WHERE NOT IN 第一个查询之类的 Doctrine 编写查询,但是查询:

    $rooms = $qb->select('ro')
            ->from('\Model\Room', 'ro')                   
            ->where($qb->expr()->notIn('ro.id', $reservations))
            ->getQuery()
            ->getResult();

给我错误:

错误:参数号无效:绑定变量的数量与标记的数量不匹配

请帮帮我。

【问题讨论】:

  • $reservations 看起来像什么?
  • array(2) { [0]=> array(1) { ["id"]=> int(2) } [1]=> array(1) { ["id"] => int(2) } }.

标签: php sql doctrine-orm


【解决方案1】:

改变

->where($qb->expr()->notIn('ro.id', $reservations))

->where($qb->expr()->notIn('ro.id', array_column($reservations, 'id')))

【讨论】:

  • 好的,我解决了这个问题。我需要一个新的 QueryBuilder 来进行第二次查询。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-23
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 2014-03-03
  • 1970-01-01
  • 2012-01-17
相关资源
最近更新 更多