Symfony2 Doctrine many-to-many bidirectional save form type

4.8k views Asked by At

I have a problem saving entities with forms. The relationship that gives me problems is many-to-many bidirectional.

Agent Entity

/**
 * @var \Doctrine\Common\Collections\ArrayCollection $agentLists
 *
 * @ORM\ManyToMany(targetEntity="AgentList", inversedBy="agents")
 * @ORM\JoinTable(name="agent_agentlist",
 *  joinColumns={@ORM\JoinColumn(name="agent_id", referencedColumnName="id")},
 *  inverseJoinColumns={@ORM\JoinColumn(name="agentlist_id", referencedColumnName="id")}
 * )
 */
protected $agentLists;

/**
 * Get agent lists
 *
 * @return \Doctrine\Common\Collections\ArrayCollection
 */
public function getAgentLists()
{
    return $this->agentLists;
}

/**
 * Add agent list to agent
 *
 * @param AgentList $agentList
 */
public function addAgentList(AgentList $agentList)
{
    $this->agentLists->add($agentList);
    $agentList->addAgent($this);
}

/**
 * Remove agent list from agent
 *
 * @param AgentList $agentList
 */
public function removeAgentList(AgentList $agentList)
{
    $this->agentLists->removeElement($agentList);
    $agentList->removeAgent($this);
}

AgentList Entity

/**
 * @var \Doctrine\Common\Collections\ArrayCollection $agents
 *
 * @ORM\ManyToMany(targetEntity="Agent", mappedBy="agentLists")
 */
protected $agents;
/**
 * Get agents
 *
 * @return \Doctrine\Common\Collections\ArrayCollection
 */
public function getAgents()
{
    return $this->agents;
}

/**
 * Add agent
 *
 * @param Agent $agent
 */
public function addAgent(Agent $agent)
{
    $this->agents[] = $agent;
}

/**
 * Remove agent
 *
 * @param Agent $agent
 */
public function removeAgent(Agent $agent)
{
    $this->agents->removeElement($agent);
}

In Agent Type

->add('agentLists', null, array('choices' => $this->commercial->getAgentLists(), 'required' => false));

In AgentList Type

->add('agents', null, array(
        'required' => false,
        'choices' => $commercial->getAgents()

It only works if I use AgentType. If I use AgentListType don't save Agents Relations! In the same way, If I:

$agent->addAgentList($agentList);

All works fine! If I:

$agentList->addAgent($agent);

don't save nothing...

3

There are 3 answers

3
DarkLeafyGreen On
$agentList->addAgent($agent);

is not enough. Since it's bidirectional you have to add/set both agent to list and list to agent.

This could work:

foreach ($agentLists as $list) {
     $alist = $em->getRepository('YourBundle:AgentList')
                   ->findOneById($list->getId());

     $agent->addAgentList($aList);
     $em->persist($agent);
     $em->flush();

     $aList->addAgent($agent);
     $em->persist($aList);
     $em->flush();
}

Remember you have to first persist one entity before you can persist and flush the other related entity.

0
Nikita Leshchev On

Unfortunately you have to put up with it

Doctrine will only check the owning side of an association for changes

https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/unitofwork-associations.html#important-concepts

1
Nick Tsarev On

For many-to-many i'm creating embed field, for example

->add('agentsEmbed', 'collection',
            array(
                'type' => new AgentListType(),
                'allow_add' => true,
                'allow_delete' => true,
                'prototype' => true,
                'property_path' => false
            )
        )

Afrer in controller:

$agents = $form['agentsEmbed']->getData();

            if($agents)
            {
                $entity->setAgentLists(new ArrayCollection());

                foreach($agents as $agent)
                {
                    $entity->addAgentList($em->getRepository('AgentList')->find($agent->getId()););
                }
            }

        $em->persist($entity);
        $em->flush();

It's works for common Many-to-Many