2016-07-29 7 views
2

"name"、 "device"、 "color"の一意のエントリを持つクラス修復があります。私。 「iPhone 5c」の「画面の置き換え」は「赤」と「緑」の色で1つだけにしてください。Doctrine:多対多のフィールドを持つUniqueEntity

しかし、symfonyは例外をスロー: 、ID_1、NAME_2 AS t0.name 、minutes_3 AS t0.minutes、safetytime_4 AS t0.safetytime AS t0.id SELECT」の実行中

例外が発生しましt0.cost AS cost_5、t0.device_id AS device_id_6 FROM repair t0 WHERE t0.name =? AND t0.device_id =? AND rep_colors.color_id =? ' のparamsと[ "画面を置換"、2、{}]:

キャッチできる致命的なエラー:教義\ ORM \ PersistentCollectionはここで文字列

に変換することができませんでした クラスのオブジェクトが私ですエンティティ定義(Repair.php):

/** 
* @ORM\Table(name="repair") 
* @ORM\Entity(repositoryClass="AppBundle\Repository\RepairRepository") 
* @UniqueEntity(fields={"name", "device", "colors"}, message="There already exists a repair job with name {{ value }} for this device and these colours.") 
*/ 
class Repair { 

    /** 
    * @var string 
    * @ORM\Column(name="name", type="string", length=255) 
    */ 
    private $name; 

    /** 
    * @ORM\ManyToOne(targetEntity="Device", inversedBy="repairs") 
    * @ORM\JoinColumn(name="device_id", referencedColumnName="id") 
    */ 
    private $device; 

    /** 
    * @ORM\ManyToMany(targetEntity="Color", inversedBy="repairs") 
    * @ORM\JoinTable(name="repairs_colors", 
    *  joinColumns={@ORM\JoinColumn(name="repair_id", referencedColumnName="id")}, 
    *  inverseJoinColumns={@ORM\JoinColumn(name="color_id", referencedColumnName="id")} 
    *  ) 
    */ 
    private $colors; 
} 

私はsymfonyは代わりに設定されている実際の色の色の空の配列を渡していることを理解しています。しかし、なぜ?

+0

を追加する見逃しているかもしれませあなたの 'Color'エンティティの' __toString'関数 –

答えて

1

Hereは、SymfonyのUniqueEntity制約のソースコードです。

->findBy(array(
'name'=>$entity->getName(), 
'device'=>$entity->getName(), 
'colors'=>$entity->getColors() // <- Precisely here, doctrine is not able to traduce that in a SQL query. 
)); 

回避策:symfonyは内部的のようなクエリを実行しますデフォルトでは

あなたが Repository methodを所有して作成し、あなたの制約でそれを使用する:

/** 
* @ORM\Table(name="repair") 
* @ORM\Entity(repositoryClass="AppBundle\Repository\RepairRepository") 
* @UniqueEntity(fields={"name", "device", "colors"}, repositoryMethod="getSimilarRepairs") 
*/ 
class Repair { 
    // ... 
} 
+0

こんにちはアルザス、あなたはExtensiveEntityBundleからの男です!ありがとう、私はそれを使用する!だから私はあなたが意味するものを得て、私は自分のDQLリポジトリコードを書こうとしています。しかし、なぜそれが 'getColors()'で失敗するのだろうか? – bluppfisk

+0

申し訳ありませんが、なぜそれが失敗するのか正確にはわかりません;) – Alsatian

関連する問題