Doctrine-无法使用多种方式更新查询

时间:2017-09-03 07:22:24

标签: php laravel symfony doctrine-orm

我想更新查询。首先,我有一个像这样的数组:

array:5 [▼
  0 => 2
  1 => 9
  2 => 10
  3 => 531
  4 => 635
]

名为$restaurants。我想要的是用这个数组更新restaurantIsSelected字段。这是我的代码

第一种方式:

public function resetChosenRestaurants(array $restaurants) {
    dd($restaurants);
    $query = $this->_em->createQueryBuilder();
    $query
        ->update($this->entityClass, "Restaurants")
        ->set("Restaurants.restaurantIsSelected", NULL)
        ->where($query->expr()->in('Restaurants.restaurantId', $restaurants))
        ->getQuery()->getSQL();
    dd($query);
}

这是我的第二种方式:

public function resetChosenRestaurants(array $restaurants) {

    $query = $this->_em->createQueryBuilder();
    $query
    ->update($this->entityClass, "Restaurants")
        ->set("Restaurants.restaurantIsSelected", NULL)
        ->where("Restaurants.restaurantId IN :restaurantId")
        ->setParameter("restaurantId", $restaurants)
        ->getQuery()->execute();
    dd($query);
}

我在这两种情况下都遇到了错误:

Error: Expected Literal, got 'WHERE'

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

用param做到:

public function resetChosenRestaurants(array $restaurants) {
    $query = $this->_em->createQueryBuilder();
    $query
        ->update($this->entityClass, "Restaurants")
        ->set("Restaurants.restaurantIsSelected", ':null')
        ->where($query->expr()->in('Restaurants.restaurantId', $restaurants))
        ->setParameter('null',null)
        ->getQuery()->execute();
}