php - Symfony3 - Combine forms -


i have 2 orm entities (customer , address) want combine 1 form. when new customer created new address must created (with type set 'invoice' default)

my current code 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 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 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 symfony book on embedding form collections returned address form every related address stored in database. when creating new customer doesn't have related addresses no (address)form elements shown. http://symfony.com/doc/current/cookbook/form/form_collections.html

an other option i've found didn't resolve without issues. how merge 2 form in symfony2

assume have business rule states every customer has @ least 1 address. can implement rule creating address in 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);   } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -