2013-03-08 2 views
6

を登録しよう私はこのクエリを持っている:教義クエリビルダは、UPDATEで作業していないとINNERが私のリポジトリで

$qb = $this->getEntityManager()->createQueryBuilder(); 
$qb 
    ->update('MyBundle:Entity1', 'e1') 
    ->join('e1.Entity2', 'e2') 
    ->set('e1.visibile', '1') 
    ->andWhere('e2.id = :id')->setParameter("id", 123) 
; 

投げ、このエラー

[Semantical Error] line 0, col 66 near 'e2.id = :id': Error: 'e2' is not defined 

私は関係をチェックして、それが正しいです。 クエリの更新でjoinを使用する際に問題はありますか?

+0

'そのクラスに' Entity2'のmemeberを持ってe1'のですか? – DonCallisto

+1

DQLを印刷して、何がうまくいかなかったのかを確認してください。 ( '$ qb-> getQuery() - > getDQL()') –

+0

あなたのエラーはあなたのコードと一致しません - エラーは ':use'を表示します。 – ManseUK

答えて

5

Doctrine DQLは更新の結合をサポートしていません。

は、以下を実行してみてください。

$qb = $this->getEntityManager()->createQueryBuilder(); 
$qb 
    ->update('MyBundle:Entity1', 'e1') 
    ->set('e1.visibile', '1') 
    ->where('e1.Entity2 = :id') 
    ->setParameter("id", 123) 
; 

あなたがいる限り、それは実体であるかのように、Doctrineはそれをマッピングします直接リンクされたエンティティの主キー、であるとして、IDを設定することができます。

私の質問では全く同じことをやっていますし、うまくいきます。

+0

私はdownvoteに値するには、私の答えについて役立つか間違っていない聞くのが大好きです。そうすれば私は自分の答えを改善することができます! –

+0

http://stackoverflow.com/questions/5594853/doctrine-2-update-with-left-join uリグ – bleuscyther

3

ではなくJoin will not work in DQL while you re doing an updateをサブクエリを使用してみてください:

LEFT JOIN, or JOINs in particular are only supported in UPDATE statements of MySQL. DQL abstracts a subset of common ansi sql, so this is not possible. Try with a subselect:

$qb = $this->getEntityManager()->createQueryBuilder(); 
    $qb ->update('MyBundle:Entity1', 'e') 
     ->set('e.visibile', '1') 
     ->where('e.id IN (SELECT e1.id FROM Entity1 e1 INNER JOIN e2.Entity2 e2 WHERE e2 = :id') 
     ->setParameter("id", 123); 
+0

それは動作しません: 'SQLSTATE [HY000]:一般的なエラー:1093指定することはできませんFROM句の更新のための 'my​​_entity1'テーブルをターゲットにします。 http://stackoverflow.com/questions/45494/mysql-error-1093-cant-specify-target-table-for-update-in-from-clause –

6

あなたはアップデートに参加してクエリを削除する使用することはできません。サブクエリを使用する必要があります。

http://www.doctrine-project.org/jira/browse/DC-646

MySQLを使用している場合

Joins are not supported on update and delete queries because it is not supported on all dbms. It won't be implemented in Doctrine 1 or Doctrine 2. You can however get the same affect by using subqueries.

、使用するサブクエリは機能しません。 2つのクエリを使用する必要があります。

In MySQL, you cannot modify a table and select from the same table in a subquery

http://dev.mysql.com/doc/refman/5.0/en/subqueries.html

+0

通常、別のテーブルと結合します。あなたは現在更新中ですので、ほとんどの場合、MySQLで動作するはずです。 –

関連する問題