[英]Symfony3 - Combine forms
I have 2 ORM entities (Customer and Address) which I want to combine into one form. 我有2个ORM实体(客户和地址),我想将它们合并为一种形式。 When a new Customer is created also an new Address must be created (with type is set to 'invoice' by default)
创建新客户时,还必须创建一个新地址(默认情况下,类型设置为“发票”)
My current code is like this. 我当前的代码是这样的。
Entities 实体
|Customers| |客户| -1----N-> |Address|
-1 ---- N-> |地址|
class Customer
{
...
/**
* @ORM\OneToMany(targetEntity="Address", mappedBy="customer")
*/
protected $addresses;
...
}
class Address
{
...
/**
* @var string
*
* @ORM\Column(name="type", type="string", length=255)
*/
private $type; // (Invoice / Delivery)
/**
* @ORM\ManyToOne(targetEntity="Customer", inversedBy="addresses")
* @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
*/
protected $customer;
...
}
The Controllers actions 管制员行动
/**
* Creates a new Customer entity.
*
* @Route("/new", name="customer_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request)
{
$customer = new Customer();
$form = $this->createForm('Rekursief\CrmBundle\Form\CustomerType', $customer);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($customer);
$em->flush();
return $this->redirectToRoute('customer_show', array('id' => $customer->getId()));
}
return $this->render('customer/new.html.twig', array(
'customer' => $customer,
'form' => $form->createView(),
));
}
/**
* Creates a new Address entity.
*
* @Route("/new", name="address_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request)
{
$address = new Address();
$form = $this->createForm('Rekursief\CrmBundle\Form\AddressType', $address);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($address);
$em->flush();
return $this->redirectToRoute('address_show', array('id' => $address->getId()));
}
return $this->render('address/new.html.twig', array(
'address' => $address,
'form' => $form->createView(),
));
}
Form types 表格类型
class CustomerType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('firstname')
->add('lastname')
->add('telephone')
->add('mobilephone')
->add('email')
;
$builder->add('type', ChoiceType::class, array(
'choices' => array(
'Corporate' => 'corporate',
'Private' => 'private',
),
'data' => 'corporate',
));
}
}
class AddressType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('street')
->add('number')
->add('box')
->add('state')
->add('country')
->add('city')
->add('postalcode')
->add('customer')
;
$builder->add('type', ChoiceType::class, array(
'choices' => array(
'Invoice' => 'invoice',
'Delivery' => 'delivery',
),
'data' => 'invoice',
));
}
}
The chapter in the symfony book on Embedding form collections returned an Address form for every related address stored in the database. symfony书籍中有关嵌入表单集合的章节为存储在数据库中的每个相关地址返回了一个“地址”表单。 But when creating a new Customer which doesn't have any related Addresses no (address)form elements are shown.
但是在创建没有任何相关地址的新客户时,不会显示(地址)表单元素。 http://symfony.com/doc/current/cookbook/form/form_collections.html
http://symfony.com/doc/current/cookbook/form/form_collections.html
An other option I've found didn't resolve without issues. 我发现的另一个选项没有问题就无法解决。 How to merge 2 form in Symfony2
如何在Symfony2中合并2表单
Assume that we have a business rule which states that every customer has at least one address. 假设我们有一条业务规则,规定每个客户至少有一个地址。 We can implement this rule by creating an address in the customer's constructor.
我们可以通过在客户的构造函数中创建地址来实现此规则。
class Customer
{
public function __construct()
{
$this->addresses = new ArrayCollection();
$address = new Address();
$this->addAddress($address);
}
public function addAddress(Address $address)
{
$this->addresses[] = $address;
$address->setCustomer($customer);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.