2017-10-25 9 views
4

多対多関係で異なる列名を持つプロパティに対して簡単な条件を使用しようとすると、Doctrineはpropertynameをフィールドとして使用します列名条件と多対多関係のクエリーで間違ったフィールド名

人異なるColumnNameに

... 
name: 
    type: string 
    nullable: false 
    length: 50 
    options: 
     fixed: false 
    column: '`key`' 
... 

人::は、hasAttribute()とのORMの定義

... 
manyToMany: 
    attributes: 
     targetEntity: Attributes 
     cascade: ['persist'] 
     joinTable: 
      name: person_attribute 
      joinColumns: 
       person_id: 
        referencedColumnName: id 
      inverseJoinColumns: 
       attribute_id: 
        referencedColumnName: id 
... 

属性ORMの定義 - 方法

$criteria = Criteria::create() 
    ->where(Criteria::expr()->eq('name', $attributeName)) 
    ->setFirstResult(0) 
    ->setMaxResults(1); 

if ($this->getAttributes()->matching($criteria)->first()) { 
    return true; 
} 

生成された声明

SELECT 
    te.id AS id, 
    te.description AS description, 
    te.key AS key 
FROM 
    attribute te 
JOIN 
    person_attribute t 
ON 
    t.attribute_id = te.id 
WHERE 
    t.person_id = ? 
     AND 
    te.name = ?  ## <- This should be "te.`key` = ?" 
+0

条件を "キー"に変更すると、DoctrineがDBからリレーションをロードする必要があるときに機能しますが、自分のユニットテストでPHP致命的になります。なぜなら、キー "または" getKey "。 – Sebus

+0

アクセスは対応するクラスのプロパティに設定されていますか?デフォルトでは、アクセスはFIELDですか? –

+0

Doctrine ORMのバージョンは何ですか? –

答えて

1

問題は機能に、 "LIB /ドクトリン/ ORM /存続/コレクション/ ManyToManyPersister.php" クラスである "loadCriteria()" の行で:

foreach ($parameters as $parameter) { 
     list($name, $value) = $parameter; 
     $whereClauses[]  = sprintf('te.%s = ?', $name); 
     $params[]   = $value; 
} 

修正がgithub linkとで添加しました現在のリリースでは、この修正プログラムを使用しないでください

foreach ($parameters as $parameter) { 
     list($name, $value) = $parameter; 
     $field = $this->quoteStrategy->getColumnName($name, $targetClass, $this->platform); 
     $whereClauses[]  = sprintf('te.%s = ?', $field); 
     $params[]   = $value; 
} 

:あなたが見ることができるようpull request

、上記のコードは次のように変更されました。 2.6リリースに含まれます。 マスターブランチでコードを使用することをお勧めします。