我的实体产品包含featureTypes的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();
}
类FeatureType:
/**
* @var Product[]|ArrayCollection
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Product", inversedBy="featureTypes")
* @ORM\JoinTable(name="products_featureTypes")
*/
private $products;
现在我想创建一个表单,它可以呈现所有可用featureType的下拉列表。我想选择一个并提交它。
我在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时,我收到一个错误:“无法确定属性'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();
}
我的前端表单显示了我的产品已有的所有featureTypes。 但我不知道,如何添加新的......
答案 0 :(得分:0)
我的属性的注释是错误的。 类产品:
/**
* @var FeatureType[]|ArrayCollection
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\FeatureType", inversedBy="products", cascade={"persist"})
* @ORM\JoinTable(name="products_featureTypes")
*/
private $featureTypes;
类FeatureType:
/**
* @var Product[]|ArrayCollection
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Product", mappedBy="featureTypes", cascade={"persist"})
*
*/
private $products;
我的表格:
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();
}
我的控制器:
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(),
];
}