1

I've already search a lot befors asking, even the related topic Symfony2-Doctrine: ManyToMany relation is not saved to database but still no answer.

I've got this two classes:

class Intervenant extends User
{

/**
 * @ManyToMany(targetEntity="iMDEO\DISAASBundle\Entity\Domaine", inversedBy="intervenants", cascade={"persist","merge"})
 */
private $domaines;


/**
 * Add domaines
 *
 * @param Domaine $domaines
 */
public function addDomaine(Domaine $domaines)
{

    $this->domaines[] = $domaines;
}

/**
 * Get domaines
 *
 * @return Doctrine\Common\Collections\Collection 
 */
public function getDomaines()
{
    return $this->domaines;
}
}
class Domaine
{
// ...
/**
 * @ORM\ManyToMany(targetEntity="Intervenant", mappedBy="domaines", cascade={"persist","merge"})
 * 
 */
private $intervenants;

/**
 * Add intervenants
 *
 * @param Intervenant $intervenants
 */
public function addIntervenant(Intervenant $intervenants)
{

    $intervenants->addDomaine($this);
    $this->intervenants[] = $intervenants;
}

/**
 * Get intervenants
 *
 * @return Doctrine\Common\Collections\Collection 
 */
public function getIntervenants()
{
    return $this->intervenants;
}
}

When I save an Intervenant, everthing is OK. But when i save the inverse side Domaine, the changes are not persisted.

Reading Symfony's doc and topics everywhere, I can't find any solution to get a bi-directionnal relation between my two entities.

Here's part of my DomaineController:

$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('myBundle:Domaine')->find($id);
if (!$entity) {
    throw $this->createNotFoundException('Unable to find Domaine entity.');
}
$editForm   = $this->createForm(new DomaineType(), $entity);
$deleteForm = $this->createDeleteForm($id);
$request = $this->getRequest();
$editForm->bindRequest($request);

if ($editForm->isValid()) {
   $em->persist($entity);
   $em->flush();
   return $this->indexAction();
}

// ...

My purpose is that when I create/edit an Intervenant, I can choose related Domaine. And when I create/edit a Domaine, I link every Intervenants in it.

Could you please help me?

Community
  • 1
  • 1
SteveM
  • 53
  • 1
  • 6
  • 1
    minor: you do not need to do $em->persist($entity), entity is manageable at this point. If you var_dump($entity) after $editForm->isValid() do you see all your relations correctly updated? – WizardZ Aug 08 '12 at 17:01
  • Yes, the collections of the entity is correct after $editForm->isValid(). The entity on the indexAction() is even correctly shown, but if i refresh the page or look into the database, nothing is saved. – SteveM Aug 09 '12 at 07:03
  • I'm having this same problem. Have you had any luck finding a solution, Steve? Thanks! – Acyra Dec 15 '12 at 12:53

0 Answers0