【问题标题】:Symfony2 Doctrine many-to-many bidirectional save form typeSymfony2 Doctrine 多对多双向保存表单类型
【发布时间】:2012-10-05 09:43:09
【问题描述】:

我在使用表单保存实体时遇到问题。给我带来问题的关系是多对多双向的。

代理实体

/**
 * @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 实体

/**
 * @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);
}

在代理类型中

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

在代理列表类型中

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

仅当我使用 AgentType 时才有效。 如果我使用 AgentListType 不要保存代理关系! 同样,如果我:

$agent->addAgentList($agentList);

一切正常! 如果我:

$agentList->addAgent($agent);

什么都不保存...

【问题讨论】:

    标签: php symfony doctrine-orm many-to-many bidirectional-relation


    【解决方案1】:
    $agentList->addAgent($agent);
    

    还不够。由于它是双向的,因此您必须将代理添加/设置为列表和列表到代理。

    这可以工作:

    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();
    }
    

    请记住,您必须先持久化一个实体,然后才能持久化并刷新另一个相关实体。

    【讨论】:

    • 为了在 AgentListType 中保存代理?
    • 什么意思?您不能保存在 agentListType 中,因为它是一个表单类。
    • 我使用 AgentListType 创建表单。我使用 Agents 选项查看选择多个,但是当我保存此表单时,关系 Agents 和 AgentsList 不起作用!
    【解决方案2】:

    例如,对于多对多,我正在创建嵌入字段

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

    在控制器中更喜欢:

    $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();
    

    它适用于常见的多对多

    【讨论】:

    • 我不明白,因为如果关系是双向的,只能保存单向
    【解决方案3】:

    不幸的是,你不得不忍受它

    Doctrine 只会检查关联的拥有方是否有更改

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-24
      • 2015-04-19
      • 1970-01-01
      相关资源
      最近更新 更多