我有2个ORM实体(客户和地址),我想将它们组合成一个表格。 创建新客户时,还必须创建新地址(默认情况下类型设置为“发票”)
我目前的代码是这样的。
实体
|客户| -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;
...
}
控制器操作
/**
* 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(),
));
}
表单类型
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',
));
}
}
关于嵌入表单集合的symfony一书中的章节为存储在数据库中的每个相关地址返回了一个地址表单。但是,当创建没有任何相关地址的新客户时,不显示(地址)表单元素。 http://symfony.com/doc/current/cookbook/form/form_collections.html
我发现的另一个选项没有解决问题。 How to merge 2 form in Symfony2
答案 0 :(得分:1)
假设我们有一个业务规则,规定每个客户至少有一个地址。我们可以通过在客户的构造函数中创建地址来实现此规则。
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);
}