【发布时间】:2017-06-25 22:45:07
【问题描述】:
我的实体产品具有特征类型的 Arraycollection (ManytoMany) 类产品:
/**
* @var FeatureType
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\FeatureType", mappedBy="products")
*/
private $featureTypes;
public function __construct()
{
$this->variants = new ArrayCollection();
$this->featureTypes = new ArrayCollection();
}
类特征类型:
/**
* @var Product[]|ArrayCollection
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Product", inversedBy="featureTypes")
* @ORM\JoinTable(name="products_featureTypes")
*/
private $products;
现在我想创建一个表单,它为我呈现所有可用特征类型的下拉列表。我想选择一个并提交。
我在 addFeatureTypeToProductType 中这样尝试过:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('featureTypes', EntityType::class, [
'class' => FeatureType::class,
'choice_label' => 'name',
])
->add('submit', SubmitType::class)
->getForm();
}
输出是包含所有可用特征类型的下拉列表。但是当我提交所选的 featureType 时,我收到一个错误:“无法确定属性 'featureType' 的访问类型”。
然后我尝试了这种方式:
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;
}
编辑 2:我用我的表单的第一种方式再次尝试。但我现在得到一个新的错误。我不知道为什么我的实体“FeatureType”不知道 contains 方法。它使用 Symfony Arraycollection
错误:试图调用类“AppBundle\Entity\FeatureType”的名为“contains”的未定义方法。
在 addFeatureTypes($featureType) 中停止调试
我现在更进一步。现在,我使用 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();
}
我的前端表单显示了我的产品已经拥有的所有功能类型。 但我不知道,如何添加一个新的...
【问题讨论】:
-
为什么在表单类型的
buildForm()方法中调用getForm()?您的Product实体的$featureTypes属性是否有getter 和setter/adder 方法? -
嗯,我是通过这种方式学习的(getForm())。它适用于所有形式。是的,我有 getter/setter 和 adder/remover,我将它添加到我的问题中 :)
-
抱歉,忘记链接@xabbuh
-
您正在尝试调用 contains() 方法
$this->featureTypes->contains($featureType)。也许我想念它,但您的 FeatureType 中没有定义此方法 -
@mickdev 我的 featureType 应该是一个 Arraycollection,具有 contains 方法。但我已经解决了这个问题:)
标签: php forms symfony formbuilder arraycollection