src/AppBundle/Controller/BarcoController.php
/**
     * Displays a form to edit an existing barco entity.
     *
     * @Route("/{id}/edit", name="barco_edit")
     * @Method({"GET", "POST"})
     */
Displays a form to edit an existing barco entity.
Displays a form to edit an existing barco entity .
public function editAction(Request $request, Barco $barco)
    {
        $em = $this->getDoctrine()->getManager();
        $barco = $em->getRepository(Barco::class)->find($barco->getId());
        $cliente = $barco->getCliente();
        if (!$barco) {
            throw $this->createNotFoundException('No hay barcos encontrados para el id '.$barco->getId());
        }
        $originalMotores = new ArrayCollection();
        foreach ($barco->getMotores() as $motor) {
            $originalMotores->add($motor);
        }
        $deleteForm = $this->createDeleteForm($barco);
        $editForm = $this->createForm('AppBundle\Form\BarcoType', $barco);
        $editForm->handleRequest($request);
        if ($editForm->isSubmitted() && $editForm->isValid()) {
            foreach ($originalMotores as $motor){
                if (false === $barco->getMotores()->contains($motor)) {
                    $motor->getBarco()->removeMotore($motor);
                    $em->persist($motor);
                     $em->remove($motor);
                }
            }
            $em->persist($barco);
            $em->flush();
            return $this->redirectToRoute('cliente_show', array('id' => $barco->getCliente()->getId()));
        }
        return $this->render('barco/edit.html.twig', array(
            'barco' => $barco,
            'cliente' => $cliente,
            'edit_form' => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }
public function editAction ( Request $ request , Barco $ barco ) { $ em = $ this -> getDoctrine ( ) -> getManager ( ) ; $ barco = $ em -> getRepository ( Barco :: class ) -> find ( $ barco -> getId ( ) ) ; $ cliente = $ barco -> getCliente ( ) ; if ( ! $ barco ) { throw $ this -> createNotFoundException ( 'No hay barcos encontrados para el id ' . $ barco -> getId ( ) ) ; } $ originalMotores = new ArrayCollection ( ) ; foreach ( $ barco -> getMotores ( ) as $ motor ) { $ originalMotores -> add ( $ motor ) ; } $ deleteForm = $ this -> createDeleteForm ( $ barco ) ; $ editForm = $ this -> createForm ( 'AppBundle\Form\BarcoType' , $ barco ) ; $ editForm -> handleRequest ( $ request ) ; if ( $ editForm -> isSubmitted ( ) && $ editForm -> isValid ( ) ) { foreach ( $ originalMotores as $ motor ) { if ( false === $ barco -> getMotores ( ) -> contains ( $ motor ) ) { $ motor -> getBarco ( ) -> removeMotore ( $ motor ) ; $ em -> persist ( $ motor ) ; $ em -> remove ( $ motor ) ; } } $ em -> persist ( $ barco ) ; $ em -> flush ( ) ; return $ this -> redirectToRoute ( 'cliente_show' , array ( 'id' => $ barco -> getCliente ( ) -> getId ( ) ) ) ; } return $ this -> render ( 'barco/edit.html.twig' , array ( 'barco' => $ barco , 'cliente' => $ cliente , 'edit_form' => $ editForm -> createView ( ) , 'delete_form' => $ deleteForm -> createView ( ) , ) ) ; }
Displays a form to edit an existing barco entity.
Displays a form to edit an existing barco entity .
// remove the Task from the Tag // if it was a many-to-one relationship, remove the relationship like this //$motor->setBarco(null); // if you wanted to delete the Tag entirely, you can also do that // redirect back to some edit page //            return $this->redirectToRoute('barco_edit', array('id' => $barco->getId()));