I have 2 ORM entities (Customer and Address) which I want to combine into one form. 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|
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. 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
An other option I've found didn't resolve without issues. How to merge 2 form in Symfony2