如何避免与Doctrine 2和Zend Framework 2的ManyToMany关系重复?

时间:2014-04-27 12:30:41

标签: orm doctrine-orm zend-framework2 many-to-many duplicates

目标是拥有2个实体文章和标签,具有多对多的关系,即使为其他文章声明相同的标签,标签表中的标签仍然是唯一的。 我尝试用代码更好地解释:

文章实体:

/**
 * @ORM\Entity
 * @ORM\Table(name="articles") 
 */
class Article {

    /**
     * @ORM\Id @ORM\GeneratedValue(strategy="AUTO") @ORM\Column(type="integer")
     * @var int 
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     * @var string
     */
    protected $name;

    /**
     * @ORM\ManyToMany(targetEntity="Tag\Entity\Tag",inversedBy="platforms",cascade={"persist","remove"})
     * 
     * @return Tag[]
     */
    protected $tags;

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

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

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

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

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

    public function setTags($tags){
        $this->tags = $tags;
    }

    public function setTagsFromArray(array $tags){
        foreach($tags as $tag){
            $this->tags->add(new Tag($tag));
        }        
    }

    /**
     * Return the associated Tags
     * 
     * @param boolean $getOnlyTagsName [optional] If set to true return a simple array of string (tags name). 
     * If set to false return array of Tag objects. 
     * 
     * @return Tag[]|string[] 
     */
    public function getTags($getOnlyTagsName=false) {
        if ($getOnlyTagsName){
            return array_map(function($i) { return $i->getName(); }, $this->tags->toArray());
        }

        return $this->tags;
    }

    public function addTags($tags) {
        foreach($tags as $tag){
            $this->tags->add($tag);
        }
    }

    public function removeTags($tags) {
        foreach ($tags as $tag){
            $this->tags->removeElement($tag);        
        }
    }  
}

标记实体:

/**
 * @ORM\Entity
 * @ORM\Table(name="tags")
 * ORM\HasLifecycleCallbacks // NOT USED
 */
class Tag {

    /**
     * @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue
     * @var int
     */
    protected $id;

    /**
     * @ORM\Column(type="string",unique=true)
     * @var string 
     */
    protected $name;

    /**
     * @ORM\Column(type="datetime",name="created_at")
     * @var datetime 
     */
    protected $createdAt;

    /**
     * @ORM\Column(type="datetime",name="updated_at")
     * @var datetime 
     */
    protected $updatedAt;

    /**
     * @ORM\ManyToMany(targetEntity="Article\Entity\Article", mappedBy="tags")
     * var Tag[] 
     */
    protected $platforms;

    /**
     * Constructor
     *
     * @param string $name Tag's name
     */
    public function __construct($name = null) {
        $this->setName($name);
        $this->setCreatedAt(new DateTime('now'));
        $this->setUpdatedAt(new DateTime('now'));
    }

    /**
     * Avoid duplicate entries.
     * 
     * ORM\PrePersist // NOT USED
     */
    public function onPrePersist(LifecycleEventArgs $args) {

    }

    /**
     * Avoid duplicate entries.
     * 
     * ORM\PreUpdate  // NOT USED
     */
    public function onPreUpdate(PreUpdateEventArgs $args) {
    }

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

    /**
     * Returns tag's id
     *
     * @return integer
     */
    public function getId() {
        return $this->id;
    }

    /**
     * Sets the tag's name
     *
     * @param string $name Name to set
     */
    public function setName($name) {
        $this->name = $name;
    }

    /**
     * Returns tag's name
     *
     * @return string
     */
    public function getName() {
        return $this->name;
    }

    public function setCreatedAt(DateTime $date) {
        $this->createdAt = $date;
    }

    public function getCreatedAt() {
        return $this->createdAt;
    }

    public function setUpdatedAt(DateTime $date) {
        $this->updatedAt = $date;
    }

    public function getUpdatedAt() {
        return $this->updatedAt;
    }    
}

然后我有Article Form(带有Article Fieldset),其中有一个tagsinput(jquery插件)元素。所以表格就像是:

object(Zend\Stdlib\Parameters)[151]
  public 'security' => string 'dc2a6ff900fbc87933e07bd35ef36709...' (length=65)
  public 'article' => 
    array (size=3)
      'id' => string '' (length=0)
      'name' => string 'Article 1' (length=13)
      'tags' => 
        array (size=3)
          0 => string 'tag1' (length=3)
          1 => string 'tag2' (length=4)
          2 => string 'tag3' (length=7)
  public 'submit' => string 'Add' (length=8)

第一个插入一切顺利,但当我尝试使用article1标签之一插入另一篇文章时,我收到错误:

An exception occurred while executing 'INSERT INTO tags (name, created_at, updated_at) VALUES (?, ?, ?)' with params ["tag2", "2014-04-26 22:05:37", "2014-04-26 22:05:37"]:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'tag2' for key 'UNIQ_6FBC94265E237E06'

我知道我可以使用prePersist和preUpdate事件列表器,但我做了一些测试,但我不知道如何使用Unit Of Work来避免重复。

1 个答案:

答案 0 :(得分:0)

您无法将标记名称属性(和数据库字段)定义为唯一。每次使用先前选择的标记名称设置新标记时,您都会遇到完整性约束违规。您只需要控制不会为与同一篇文章相关的标记选择名称。

您可以在 tagsinput 元素中添加 Zend\Validator\Db\NoRecordExists 验证程序来实现这一目标。它会是这样的:

$tagsinput->getValidatorChain()->addValidator(
    new NoRecordExists( array(
    'adapter' => $this->getServiceManager()->get( 'Zend\Db\Adapter\Adapter' ),
    'table' => 'tag',
    'field' => 'name',
    'message' => 'The tag name already exists', 
    'exclude' => 'article_id = ' . $article->getId(),
) );

-

修改 如果有多个标记,当标记输入的值为数组时,此解决方案将无法运行。您可以遍历数组来验证每个标记,但它不是一个有效的解决方案,它会执行 N 查询。你可以实现一个类似的验证器,它接受一个where子句,比如 TAG_NAME IN(&#39; tag1&#39;,&#39; tag2&#39;,&#39; tag3&#39;) < / p>

-

请注意,此验证程序不适用于Doctrine2; 字段排除参数具有数据库值。

在向现有文章添加标签时,这应该有效。要验证新文章没有重复的标记,您可以在客户端执行,也可以write your own validator