首页 > 解决方案 > 为每个产品字段添加一个子字段 - Symfony Forms

问题描述

我正在尝试使用 Symfony 4.4 Forms 构建订单功能

我有这个,到目前为止

class OrderProductForm extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('customer', EntityType::class, [
                'class' => 'App\Entity\Customers',
                'choice_label' => 'name',
                'label' =>'order_form.name'
            ])
            ->add('products', CollectionType::class, [
                'entry_type' => ProductQuantityType::class,
                'label' => '',
            ])
            ->getForm();

    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'App\Entity\Order',
        ));
    }
}

class ProductQuantityType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('products', EntityType::class, [
                'class' => 'App\Entity\Products',
                'expanded' => true,
                'multiple' => true,
                'choice_label' => 'name',
                'label' =>''
            ])
            ->add('qty', IntegerType::class, [
                'label' =>'order_form.qty'
            ])
            ->getForm();

    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => null,
        ));
    }

}

这不会呈现产品数量,因为它没有数据。Symfony 手册没有说怎么做... CollectionType

我真正想要的是为每个产品添加一个数量字段,同时添加我想要的任意数量的产品。

这是教义结构

仅包含相关代码

class Order
{
   /**
     * @ORM\ManyToOne(targetEntity=Customers::class, inversedBy="orders")
     * @ORM\JoinColumn(nullable=false)
     */
    private $customer;
}
class Customers
{
   /**
     * @ORM\OneToMany(targetEntity=Order::class, mappedBy="customer", orphanRemoval=true)
     */
    private $orders;
}

关于我如何继续前进的任何想法?谢谢 !

标签: phpsymfonydoctrinesymfony4symfony-forms

解决方案


推荐阅读