Doctrine orm 使用联接更新条令查询生成器

Doctrine orm 使用联接更新条令查询生成器,doctrine-orm,Doctrine Orm,我必须使用多对多关联字段(groupes)上的条件更新某些实体(StatistiqueColle) 以下是我提出的目前不起作用的查询: $qb = $this->em->createQueryBuilder(); $qb->update('PACESStatistiqueBundle:StatistiqueColle', 's') ->set('s.banque', 1) ->set('s.statsEntrainement'

我必须使用多对多关联字段(groupes)上的条件更新某些实体(StatistiqueColle)

以下是我提出的目前不起作用的查询:

$qb = $this->em->createQueryBuilder();
    $qb->update('PACESStatistiqueBundle:StatistiqueColle', 's')
        ->set('s.banque', 1)
        ->set('s.statsEntrainement', 0)
        ->andWhere($qb->expr()->in('s.groupes', 'g'))
        ->andWhere($qb->expr()->eq('s.banque', ':banque'))
        ->andWhere($qb->expr()->in('g.id', ':salle'))
        ->andWhere($qb->expr()->eq($qb->expr()->count('g.id'), ':count'))
        ->setParameters(['salle' => $groupeSalle, 'banque' => 0, 'count' => 1]);
    $qb->getQuery()->execute();

我看到条令不接受加入更新。如果没有联接,我如何做我想做的事情?

更新和删除查询不支持联接,因为并非所有dbms都支持联接。它不会在第1条或第2条中实施。但是,您可以通过使用子查询获得相同的效果

另一种解决方案是将其分为两个查询:

  • 选择要更新的所有记录
  • 更新记录
像这样:

$qb = $this->em->createQueryBuilder();
$results = $qb->select('PACESStatistiqueBundle:StatistiqueColle', 's')
    ->andWhere($qb->expr()->in('s.groupes', 'g'))
    ->andWhere($qb->expr()->eq('s.banque', ':banque'))
    ->andWhere($qb->expr()->in('g.id', ':salle'))
    ->andWhere($qb->expr()->eq($qb->expr()->count('g.id'), ':count'))
    ->setParameters(['salle' => $groupeSalle, 'banque' => 0, 'count' => 1]);
$qb->getQuery()->getResult();

foreach ($results ad $result) {
    $result->setBanque(1);
    $result->setStatsEntrainement(1);
    $this->em->persist($result);
    $this->em->flush();
}

update和delete查询不支持联接,因为并非所有dbms都支持联接。它不会在第1条或第2条中实施。但是,您可以通过使用子查询获得相同的效果

另一种解决方案是将其分为两个查询:

  • 选择要更新的所有记录
  • 更新记录
像这样:

$qb = $this->em->createQueryBuilder();
$results = $qb->select('PACESStatistiqueBundle:StatistiqueColle', 's')
    ->andWhere($qb->expr()->in('s.groupes', 'g'))
    ->andWhere($qb->expr()->eq('s.banque', ':banque'))
    ->andWhere($qb->expr()->in('g.id', ':salle'))
    ->andWhere($qb->expr()->eq($qb->expr()->count('g.id'), ':count'))
    ->setParameters(['salle' => $groupeSalle, 'banque' => 0, 'count' => 1]);
$qb->getQuery()->getResult();

foreach ($results ad $result) {
    $result->setBanque(1);
    $result->setStatsEntrainement(1);
    $this->em->persist($result);
    $this->em->flush();
}

我使用了您的解决方案,但我用更新查询和where in条件替换了循环。我还有一个问题:第一个查询没有按预期工作。计数条件不起作用。当我添加此条件时,它不会返回任何结果,而我检查了,并且有许多结果与StatistiqueColle相对应,仅链接到$GroupsAlle我使用了您的解决方案,但我用更新查询和where in条件替换了循环。我还有一个问题:第一个查询没有按预期工作。计数条件不起作用。当我添加此条件时,它不会返回任何结果,而我进行了检查,并且有许多结果与仅链接到$groupeSalle的StatistiqueColle相对应