Symfony2:ManyToMany关系和Collection表单类型

时间:2012-05-31 11:31:26

标签: symfony doctrine-orm

我在Symfony2项目中遇到了ManyToMany关系的Collection表单类型的大问题。

环境: - Symfony 2.0.14 - Doctrine 2.1

以下是一些代码:

发布实体

class Post
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\Column(type="string", length=255, nullable=false)
 */
private $title;

/**
 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="posts", cascade={"persist"})
 * @ORM\JoinTable(name="posts_tags")
 */
private $tags;

public function setTags(\Doctrine\Common\Collections\ArrayCollection $tags)
{
    foreach($tags as $tag)
    {
        $tag->addSnippet($this);
    }
}

public function addTag(\My\BlogBundle\Entity\Tag $tags)
{
    $this->tags[] = $tags;
}

public function getTags()
{
    return $this->tags;
}

标记实体

class Tag
{
/**
 * @var integer $id
 * 
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string $name
 * 
 * @ORM\Column(type="string", length=100, unique=true, nullable=false)
 */
private $name;

/**
 * @var Snippet
 * 
 * @ORM\ManyToMany(targetEntity="Post", mappedBy="tags") 
 */
private $posts;

public function addSnippet(\My\BlogBundle\Entity\Post $posts)
{
    $this->posts[] = $posts;
}

PostType表单类

->add('tags', 'collection', array(
            'type' => new TagType(),
            'allow_add' => true,
            'prototype' => true,
            'by_reference' => false,
        ))

一切正常,但在插入数据库SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'tag1' for key 'UNIQ_6FBC94265E237E06'中已存在的标记时会引发错误。

你有没有解决这个问题的方法,或者我错过了什么?我的控制器是由app/console生成的标准CRUD控制器。enter code here

1 个答案:

答案 0 :(得分:4)

好吧,我并没有太多关于学说,但似乎你在同一个集合中插入一个具有相同ID /名称的标签。我想你可以在插入

之前检查标签是否存在
public function existTags(\Doctrine\Common\Collections\ArrayCollection $tags)
{
    foreach($this->tags as $tag)
    {
        if ( $tag->getID() === $tags->getId() )
            return true;
    }
    return false;
}

然后

public function addTag(\My\BlogBundle\Entity\Tag $tags)
{
    if ( !$this->existTag($tags) );
        $this->tags[] = $tags;
}

这些是我的模特:

namespace models;


/**
 * @Entity
 * @Table(name="tag")
 */
class Tag
{

    /**
     * @Id @Column(type="integer", nullable=false, name="id")
     * @GeneratedValue(strategy="AUTO")
     */
    protected $id;


    /**
     * @Column(type="string", nullable=false)
     */
    protected $name;

    public function getId(){ return $this->id; }
    public function getName(){ return $this->name; }

    public function setId($id){ $this->id = $id; }
    public function setName($name){ $this->name = $name; }


}

博客条目:

namespace models;

use \Doctrine\Common\Collections\ArrayCollection;

/**
 * @Entity
 * @Table(name="entry")
 */
class Entry
{

    /**
     * @Id @Column(type="integer", nullable=false, name="id")
     * @GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @Column(type="string", nullable=false, name="body")
     */
    protected $body;



    /**
     * @ManyToMany(targetEntity="Tag")
     * @JoinTable(name="entry_tagged",
     *      joinColumns={@JoinColumn(name="entry_id", referencedColumnName="id")},
     *      inverseJoinColumns={@JoinColumn(name="tag_id", referencedColumnName="id")}
     *      )
     */
    private $tags;





    public function __construct(){
        $this->tags = new ArrayCollection();
    }

    public function existTag(models\Tag $tag)
    {
        foreach($this->tags as $temp)
        {
            if ( $tag->getID() === $temp->getId() )
                return true;
        }
        return false;
    }

    public function addTag(models\Tag $tag)
    {
        if ( !$this->existTag($tag) );
            $this->tags->add($tag);
    }


    public function getId(){ return $this->id; }
    public function getBody(){ return $this->body; }
    public function getTags(){ return $this->tags; }

    public function setId($id){ $this->id = $id; }
    public function setBody($body){ $this->body = $body; }
    public function setTags($tags){ $this->tags = $tags; }

}

最后,使用的连接表是“entry_tagged”,如下所示:

entry_id | tag_id