2016-03-31 12 views
0

私は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.

感謝。

答えて

0

EDIT: 申し訳ありませんが、属性を持つManyToManyが必要なことを理解できませんでした。ProductCategoryTypeを作成する必要があると思いますが、「属性」のフィールドとカテゴリのフィールドEntityTypeがあります。 ProductTypeにProductCategoryTypeのCollectionTypeを追加します。


あなたのエンティティを使用すると、多対多の関係、及び各エンティティにはOneToMany、他のエンティティにそのマッハを使用することができ、正しくありません。それ以外の場合、Doctrineは2つのエンティティ間の関係を行いません。 ManyToMany関係を持つProuctとCategoryエンティティが必要です。 ManyToMany双方向の例を示します。

あなたが製品の一部のメソッドを追加する必要がある
class Product { 
    /** 
    * @ORM\Column(type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * @ORM\ManyToMany(targetEntity="Category", mappedBy="products") 
    */ 
    private $categories; 

    //.. 


class Category { 
    /** 
    * @ORM\Column(type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * @ORM\ManyToMany(targetEntity="Product", inversedBy="categories") 
    */ 
    private $products; 

    //.. 

:。

__construct() { 
    $this->categories = new ArrayCollection(); 
} 

addCategory(Category $category) { 
    $this->categories[] = $category; 
} 

removeCategory(Category $category) { 
    $this->categories->removeElement($caegory); 
} 

getCategories() { 
    return $this->categories(); 
} 

は、その後、あなたがこのようなaddCategory方法を変更することができますカテゴリ別製品とカテゴリによる分類を置き換える(カテゴリーために同じ操作を行います

addCategory(Category $category) { 
    $this->categories[] = $category; 
    $category->addProduct($this); 
} 

http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-many-unidirectional

http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-many-bidirectional

+0

いいえ、いいえ、いいえ、わかりません。例では、新たに作成された第3のテーブルにフィールドを追加することはできません。 http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-many-unidirectional '多対多の関連性があまり一般的でないのはなぜですか?追加のアトリビュートをアソシエーションに関連付けたい場合が多いため、アソシエーションクラスを導入します。その結果、多対多の直接的な関連付けは消滅し、3つの参加クラス間の1対多/多対1の関連付けに置き換えられます。 – nacholibre

+0

申し訳ありませんが、属性を持つManyToManyが必要なことを理解していません。私はProductCategoryTypeを作成する必要があると思う、あなたの "属性"のためのいくつかのフィールドとカテゴリーのフィールドEntityType。次に、ProductTypeにProductCategoryTypeのCollectionTypeを追加します。 – mpiot

関連する問題