[英]Symfony FormType, get Dropdown from Arraycollection
i have my Entity Product with an Arraycollection of featureTypes (ManytoMany) Class Product: 我有我的实体产品与featureTypes(ManytoMany)类产品的Arraycollection:
/**
* @var FeatureType
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\FeatureType", mappedBy="products")
*/
private $featureTypes;
public function __construct()
{
$this->variants = new ArrayCollection();
$this->featureTypes = new ArrayCollection();
}
Class FeatureType: 类FeatureType:
/**
* @var Product[]|ArrayCollection
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Product", inversedBy="featureTypes")
* @ORM\JoinTable(name="products_featureTypes")
*/
private $products;
Now i want to create a form which render me a dropdown of all available featureTypes. 现在我想创建一个表单,它可以呈现所有可用featureType的下拉列表。 I want to select one and submit it.
我想选择一个并提交它。
I tried it like this in my addFeatureTypeToProductType: 我在addFeatureTypeToProductType中尝试了这样的:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('featureTypes', EntityType::class, [
'class' => FeatureType::class,
'choice_label' => 'name',
])
->add('submit', SubmitType::class)
->getForm();
}
The output is the Dropdown with all available FeatureTypes. 输出是包含所有可用FeatureType的Dropdown。 But when I submit the selected featureType, i get an error: "Could not determine access type for property 'featureType'".
但是当我提交所选的featureType时,我收到一个错误:“无法确定属性'featureType'的访问类型”。
Then I tried this way: 然后我试着这样:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('featureTypes', CollectionType::class, [
'entry_type' => FeatureType::class,
'allow_add' => true,
])
->add('submit', SubmitType::class)
->getForm();
}
But this does not work 但这不起作用
My Controller: 我的控制器:
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()
];
}
Sorry for my english :S 对不起我的英文:S
EDIT: Here are my adder/remover and setter/getter: 编辑:这是我的加法器/去除器和setter / getter:
/**
* @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: I tried it again with the first way of my form. 编辑2:我用我的形式的第一种方式再次尝试。 But i get a new Error now.
但我现在得到一个新的错误。 I don' know why my entity "FeatureType" don't knows the contains method.
我不知道为什么我的实体“FeatureType”不知道contains方法。 It uses the Symfony Arraycollection
它使用Symfony Arraycollection
Error: Attempted to call an undefined method named "contains" of class "AppBundle\\Entity\\FeatureType". 错误:尝试调用类“AppBundle \\ Entity \\ FeatureType”的名为“contains”的未定义方法。
Debugging stops in addFeatureTypes($featureType) 调试在addFeatureTypes($ featureType)中停止
Im one step further now. 我现在更进一步了。 Now, I uses the collectionType.
现在,我使用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();
}
My frontend form shows all featureTypes, which my product already has. 我的前端表单显示了我的产品已有的所有featureTypes。 But i don't know, how too add a new one...
但我不知道,如何添加新的......
The annotation of my properties were wrong. 我的属性的注释是错误的。 Class Product:
类产品:
/**
* @var FeatureType[]|ArrayCollection
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\FeatureType", inversedBy="products", cascade={"persist"})
* @ORM\JoinTable(name="products_featureTypes")
*/
private $featureTypes;
Class FeatureType: 类FeatureType:
/**
* @var Product[]|ArrayCollection
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Product", mappedBy="featureTypes", cascade={"persist"})
*
*/
private $products;
My Form: 我的表格:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('featureTypeToAdd', EntityType::class, [
'class' => FeatureType::class,
'choice_label' => 'name',
'mapped' => false,
])
->add('submit', SubmitType::class)
->getForm();
}
And my Controller: 我的控制器:
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('featureTypeToAdd')->getData();
$em = $this->getDoctrine()->getManager();
$product->addFeatureTypes($featureType);
$em->persist($product);
$em->flush();
return $this->redirectToRoute('admin_products_list_all');
}
return [
'form' => $form->createView(),
];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.