Symfony2 keep correlated objects

This is my problem: Entity Address Book 1-N Entity Number

The controller displays an edit form with AddressBook and its number, but when I save the form I get this error: Fatal error: Calling the setTipo () member function on a non-object

Strange, however, the data is saved correctly

This code of mine:

 /**
 * Modifica dati Anagrafica
 * @Route("/contatto/{id}/modifica", name="_anagrafica_modifica")
 * @Template()
 */
public function modificaAction($id)
{
    $em = $this->getDoctrine()->getManager();
    $anagrafica = $em->getRepository('MercurioInterfaceBundle:Anagrafica')->find($id);

    if (!$anagrafica) {
        throw $this->createNotFoundException('No anagrafica found for id '.$id);
    }

    $form = $this->createForm(new \Mercurio\InterfaceBundle\Form\Anagrafica\FormAnagrafica(), $anagrafica);

    $request = $this->getRequest();

    if ($request->getMethod() == 'POST')
    {
        $form->bind($request);
        if ($form->isValid())
        {
            $chiave = $request->request->get('anagrafica');

            $em = $this->getDoctrine()->getManager();
            $anagrafica = $em->getRepository('MercurioInterfaceBundle:Anagrafica')->find($id);

            $anagrafica->setNominativo($chiave['nominativo']);
            $anagrafica->setIndirizzo($chiave['indirizzo']);
            $anagrafica->setCap($chiave['cap']);
            $anagrafica->setCitta($chiave['citta']);
            $anagrafica->setNote($chiave['note']);
            $em->flush();

            $dettaglio = $em->getRepository('MercurioInterfaceBundle:AnagDettaglio')->findBy(array('anagrafica_id' => $id,));
            foreach ($chiave['anag_dettagli'] as $d)
            {
                $dettaglio->setTipo($d['tipo']);
                $dettaglio->setValore($d['valore']);
                $dettaglio->setRiferimento($d['riferimento']);
            }
            $em->flush();

            return $this->redirect($this->generateUrl('_anagrafica_contatto', array('id' => $id)));
        }
    }

    return array(
        'form' => $form->createView(),
        'id' => $id
    );
}

      

+3


source to share


1 answer


Solved:



$form = $this->createForm(new \Mercurio\InterfaceBundle\Form\Anagrafica\FormAnagrafica(),$anagrafica);
if ($request->isMethod('POST')){
  $form->bind($request);

  if ($form->isValid()) {
    $em->persist($anagrafica);
    $em->flush();
    return $this->redirect(....)
  }
}

      

0


source







All Articles