からドロップダウンを取得:symfonyのにformType、私はfeatureTypesのArrayCollectionの(多対多) クラスの製品と私のエンティティの製品を持っているのArrayCollection
/**
* @var FeatureType
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\FeatureType", mappedBy="products")
*/
private $featureTypes;
public function __construct()
{
$this->variants = new ArrayCollection();
$this->featureTypes = new ArrayCollection();
}
クラスFeatureType:
/**
* @var Product[]|ArrayCollection
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Product", inversedBy="featureTypes")
* @ORM\JoinTable(name="products_featureTypes")
*/
private $products;
今私が作成したいです私は利用可能なすべてのfeatureTypesのドロップダウンをレンダリングします。私は1つを選択して提出したいと思います。
私は私のaddFeatureTypeToProductTypeでこのようにそれを試してみました:出力は、使用可能なすべてのFeatureTypesとドロップダウンで
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('featureTypes', EntityType::class, [
'class' => FeatureType::class,
'choice_label' => 'name',
])
->add('submit', SubmitType::class)
->getForm();
}
。しかし、私が選択したフィーチャタイプを送信すると、「フィーチャタイプ 'のフィーチャタイプのアクセスタイプを決定できませんでした」というエラーが表示されます。
は、その後、私はこの方法を試してみました:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('featureTypes', CollectionType::class, [
'entry_type' => FeatureType::class,
'allow_add' => true,
])
->add('submit', SubmitType::class)
->getForm();
}
しかし、これは
私のコントローラを動作しません:
私の英語のため申し訳ありませんpublic function addFeatureTypeAction(Request $request, Product $product)
{
$form = $this->createForm(AddFeatureTypeToProductType::class, $product, [
'action' => $this->generateUrl('admin_products_add_featureTypes', [
'product' => $product->getId()
])
]);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$featureType = $form->get('featureTypes');
$product->addFeatureTypes($featureType);
$em = $this->getDoctrine()->getManager();
$em->persist($product);
$em->flush();
return $this->redirectToRoute('admin_products_list_all');
}
return [
'form' => $form->createView()
];
}
:S
編集:ここにあります私の加算器/リムーバーとセッター/ゲッタ:
/**
* @return FeatureType
*/
public function getFeatureTypes()
{
return $this->featureTypes;
}
/**
* @param FeatureType $featureTypes
*/
public function setFeatureTypes($featureTypes)
{
$this->featureTypes = $featureTypes;
}
/**
* Add new FeatureType
*
* @param FeatureType $featureType
*
* @return Product
*/
public function addFeatureTypes($featureType)
{
if (!$this->featureTypes->contains($featureType)) {
$this->featureTypes->add($featureType);
}
return $this;
}
/**
* @param FeatureType $featureType
*
* @return Product
*/
public function removeFeatureTypes($featureType)
{
if ($this->featureTypes->contains($featureType)) {
$this->featureTypes->remove($featureType);
}
return $this;
}
EDIT 2:私のフォームの最初の方法でもう一度試しました。しかし、今私は新しいエラーを取得します。私のエンティティ "FeatureType"がcontainsメソッドを知らない理由は分かりません。それはSymfony Arraycollectionを使用します
エラー:クラス "AppBundle \ Entity \ FeatureType"の "contains"という名前の未定義メソッドを呼び出そうとしました。
デバッグ($ featureType)addFeatureTypesで停止
はさらに一歩今イム。今、私はcollectionTypeを使用します。
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('featureTypes', CollectionType::class, [
'entry_type' => FeatureTypeType::class,
'allow_add' => true,
])
->add('submit', SubmitType::class)
->getForm();
}
私のフロントエンドフォームには、自分の製品が既に持っているすべてのfeatureTypesが表示されます。 しかし、わからない、新しいものを追加する方法...
あなたは 'をgetForm(コールなぜ)'あなたのフォームタイプの 'buildForm()' metの中でホド?あなたの 'Product'エンティティの' $ featureTypes'プロパティに対するgetterメソッドとsetter/adderメソッドがありますか? – xabbuh
さて、私はこのように(getForm())それを学んだ。それはすべてのフォームで動作します。 はい、getter/setterと加算器/除去器があります。私は質問に追加しました:) –
@xabbuh –