Symfony2教义多对多双向保存表单类型 [英] Symfony2 Doctrine many-to-many bidirectional save form type

查看:153
本文介绍了Symfony2教义多对多双向保存表单类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

保存具有表单的实体时出现问题。给我问题的关系是多对多双向的。



代理实体

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

/ **
*获取代理列表
*
* @return \Doctrine\Common\Collections\ArrayCollection
* /
public function getAgentLists()
{
return $ this-> agentLists;
}

/ **
*将代理列表添加到代理
*
* @param AgentList $ agentList
* /
public function addAgentList(AgentList $ agentList)
{
$ this-> agentLists-> add($ agentList);
$ agentList-> addAgent($ this);
}

/ **
*从代理中删除代理列表
*
* @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;
/ **
*获取代理
*
* @return \Doctrine\Common\Collections\ArrayCollection
* /
public function getAgents()
{
return $ this-> agents;
}

/ **
*添加代理
*
* @param代理$代理
* /
公共函数addAgent(Agent $ agent)
{
$ this-> agents [] = $ agent;
}

/ **
*删除代理
*
* @param代理$代理
* /
公共函数removeAgent(Agent $ agent)
{
$ this-> agents-> removeElement($ agent);
}

代理类型

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

在代理列表类型

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

只有在我使用AgentType的情况下才有效
如果我使用AgentListType不保存代理关系!
以同样的方式,如果我:

  $ agent-> addAgentList($ agentList); 

一切正常!
如果我:

  $ agentList-> addAgent($ agent); 

不保存任何东西...

解决方案

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

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


控制器中的Afrer:

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

它适用于常见的多对多


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...

解决方案

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

这篇关于Symfony2教义多对多双向保存表单类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆