2016-09-01 17 views
0

PostCategoryの間には、manyToManyの関連があります。投稿フォームを送信して、選択したカテゴリの投稿をドロップダウン<select>から作成しようとしています。エラー:新しい値は Traversable、Entityクラスの配列またはインスタンスでなければなりません

<div id="form-categories-widget"> 
    <select id="shop_bundle_managementbundle_posttype_categories" 
     name="shop_bundle_managementbundle_posttype[categories]"> 
     {% for key,val in form.categories.vars.choices %} 
     <option value="{{ val.value }}" {{ form.categories.vars.value == '' and key == 0 ? ' selected ' :(val.value == form.categories.vars.value ? ' selected ' : '') }} 
      > 
      {{ val.data.getName | trans }} 
     </option> 
     {% endfor %} 
    </select> 
</div> 

問題を::フォームが使用してcorrecltyをレンダリングしている

私は私が把握しようと、約2日間を過ごしてきたために、私は(次のようなエラーを持って送信ボタンをクリックしてください):クラス で

プロパティ "カテゴリ" "ショップ\バンドル\ ManagementBundle \エンティティ\ポスト紙は"で定義することができますメソッド "addCategory()"、 "removeCategory()"を使用しますが、新しい値は 配列または\ Traversableのインスタンスである必要があります。 "Shop \ Bundle \ ManagementBundle \ Entity \ Category"が指定されています。

ここに私のフォームタイプとエンティティがあります(便利な場合)。私はあなたの貴重な時間のために事前に感謝し、いつものように助けてください。

エンティティポスト:

<?php 

namespace Shop\Bundle\ManagementBundle\Entity; 

class Post 
{ 
..... 
    /** 
    * @var \Doctrine\Common\Collections\Collection 
    */ 
    private $categories; 


    /** 
    * Add category 
    * 
    * @param \Shop\Bundle\ManagementBundle\Entity\Category $category 
    * 
    * @return Post 
    */ 
    public function addCategory(\Shop\Bundle\ManagementBundle\Entity\Category $category) 
    { 
     $this->categories[$category->getName()] = $category; 
     $category->addPost($this); 
     return $this; 
    } 

    /** 
    * Remove category 
    * 
    * @param \Shop\Bundle\ManagementBundle\Entity\Category $category 
    */ 
    public function removeCategory(\Shop\Bundle\ManagementBundle\Entity\Category $category) 
    { 
     $this->categories->removeElement($category); 
    } 

    /** 
    * Get categories 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getCategories() 
    { 
     return $this->categories; 
    } 


} 

PostType

class PostType extends AbstractType 
{ 
    /** 
    * @param FormBuilderInterface $builder 
    * @param array $options 
    */ 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('categories', 'entity', array(
       'multiple' => false, // Multiple selection allowed 
       'expanded' => false, // Render as checkboxes 
       'class' => 'ShopManagementBundle:Category', 
       'property'  => 'name' 
      )); 

    } 
    /** 
    * @param OptionsResolver $resolver 
    */ 
    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'Shop\Bundle\ManagementBundle\Entity\Post' 

     )); 
    } 
    /** 
    * @return string 
    */ 
    public function getBlockPrefix() 
    { 
     return 'shop_bundle_managementbundle_posttype'; 
    } 

} 

答えて

1

私は手動では、二重括弧[]を忘れ符号化されたため、問題は、フォームテンプレートから来ていました。だから、代わりに:

<select id="shop_bundle_managementbundle_posttype_categories" 
     name="shop_bundle_managementbundle_posttype[categories]"> 

私が使用している必要があります。

<select id="shop_bundle_managementbundle_posttype_categories" 
     name="shop_bundle_managementbundle_posttype[categories][]"> 

私は他の人が同じミスをしないことを望みます。

おかげで私の理解からも

1

エンティティのCollectionTypeはだけではなく、実体であるためにあなたのフィールド "のカテゴリーのニーズ。

この例では、カテゴリエンティティによるコレクションである$ categoriesを置き換えようとしています。

この読み:CollectionType Field

をしかし、私は誤解があるという意見であった:あなたは、単一のポストに複数のカテゴリを与えることをしようと? これを元に戻しましたか? 1つのカテゴリに複数の投稿が含まれていますか?

http://symfony.com/doc/current/reference/forms/types/collection.html

+0

あなたは子entitesの新しいインスタンスを作成する必要がある場合、コレクション型ですが、すでに作成した子エンティティの定義済みのセットを持っている場合、使用してEntityTypeがより適切です。両方のケースで 'form.categories.vars'をダンプすると、2番目のケースには、既存のカテゴリを含む変数' choices'があります。あなたの質問について:はい私は反対側(addPost、removePost、getPostsとコンストラクタのarrayColletcionとして$ postsの初期化)も持っています。あなたの時間をありがとう –

関連する問題