私は3つの単純なエンティティを持っています。多対多関連3参加クラス - フォーム生成
class Product {
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="ProductsCategories", mappedBy="category")
*/
private $categories;
//..
class ProductsCategories {
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Product")
*/
private $product;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Category")
*/
private $category;
//..
class Category {
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="ProductsCategories", mappedBy="product")
*/
private $products;
//..
追加のフィールドを追加するために、追加のクラスとの多対多の関連付けを作成します。私の問題は、私は、フォームフィールドを生成する方法がわからないということです、ここに私のフォームタイプ
//..
->add('categories', 'entity', [
'class' => 'AppBundle:Category',
'choice_label' => function($category, $key, $index) {
return $category->getName();
},
'multiple' => true,
'required' => true,
'expanded' => false,
])
//..
である。しかし、私は、フォームを送信すると、このエラーが現れます:
Expected value of type "Doctrine\Common\Collections\Collection|array" for association field "AppBundle\Entity\Product#$categories", got "AppBundle\Entity\Category" instead.
感謝。
いいえ、いいえ、いいえ、わかりません。例では、新たに作成された第3のテーブルにフィールドを追加することはできません。 http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-many-unidirectional '多対多の関連性があまり一般的でないのはなぜですか?追加のアトリビュートをアソシエーションに関連付けたい場合が多いため、アソシエーションクラスを導入します。その結果、多対多の直接的な関連付けは消滅し、3つの参加クラス間の1対多/多対1の関連付けに置き換えられます。 – nacholibre
申し訳ありませんが、属性を持つManyToManyが必要なことを理解していません。私はProductCategoryTypeを作成する必要があると思う、あなたの "属性"のためのいくつかのフィールドとカテゴリーのフィールドEntityType。次に、ProductTypeにProductCategoryTypeのCollectionTypeを追加します。 – mpiot