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