【问题标题】:update doctrine with join table使用连接表更新学说
【发布时间】:2012-05-24 19:59:37
【问题描述】:

我正在尝试更新 Note Entity 中的字段“note”,该字段与“ElementModule”和“Inscription”具有多对一(双向)关系,实体“Inscription”与“Etudiant”实体具有多对一关系

我试过这个 DQL 查询:

$query = $this->_em->createQuery('update UaePortailBundle:Note u JOIN u.inscription i JOIN u.elementmodule e join i.etudiant et set u.note = ?3
                                                where et.id = ?1 and e.id = ?2 ');
    $query->setParameter(1, $etudiant);
    $query->setParameter(2, $element);
    $query->setParameter(3, $note);
    $resultat = $query->execute();

我收到这个错误

[Syntax Error] line 0, col 50: Error: Expected Doctrine\ORM\Query\Lexer::T_EQUALS, got 'i'

【问题讨论】:

    标签: symfony doctrine-orm


    【解决方案1】:

    LEFT JOIN,特别是 JOIN 仅在 MySQL 的 UPDATE 语句中受支持。 DQL 抽象了普通 ansi sql 的一个子集,所以这是不可能的。尝试使用子选择或 IDENTITY(您必须使用最新版本的 Doctrine 2.2.2):

    createQuery('update UaePortailBundle:Note u set u.note = ?3
                 where IDNETITY(u.inscription) = ?1 and IDENTITY(u.elementmodule) = ?2 ');
    

    【讨论】:

    • 我试过这个,但是,它不起作用,因为我的学说版本是 2.0
    • 如果你想使用 DQL,你必须更新你的学说版本到 2.2.2 或者你可以使用 NativeQuery :)
    【解决方案2】:

    在使用教义dql搜索并尝试找到解决方案后,我无法找到它,所以我使用直接sql查询来更新数据库。

         $stmt = $this->getEntityManager()
                   ->getConnection()
                   ->prepare("update note set note = :note where `etudiant_id` like :etudiant_id and `elementmodule_id` like :elementmodule_id");
                    $stmt->bindValue('etudiant_id',$etudiant);
                    $stmt->bindValue('elementmodule_id' ,$element );
                    $stmt->bindValue('note', $note);
                    $stmt->execute();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多