php - 为每个产品字段添加一个子字段 - 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;
}
关于我如何继续前进的任何想法?谢谢 !
解决方案
推荐阅读
- javascript - 以下哪种方法是明智地使用以下语句的正确方法?
- d3.js - 如何使用 Simg 库返回 base64 图像数据
- ansible - 又一次 Ansible 迭代
- python-3.x - 如何在我的代码中的特定点读取 Kivy.properties.NumericProperty 值
- c# - Visual Studio 2017 不会在 ASP.NET Core MVC 项目中创建新的空 MVC 控制器
- python - 在 Dask 数据框操作中引用新创建的列
- reactjs - 反应原生导航动作重置
- javascript - 意外的反应组件状态值更改
- wordpress - Get the var from url
- reactjs - 使用 moment 将日期设置为不是从当前日期开始的月份