3

在我的 Symfony2 项目中,我面临着多对多关系的 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.enter code here

4

1 回答 1

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
于 2012-05-31T12:16:35.460 回答