教义2 @ Gedmo\Soft可删除和唯一的字段 [英] Doctrine 2 @Gedmo\SoftDeleteable and unique fields

查看:101
本文介绍了教义2 @ Gedmo\Soft可删除和唯一的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我有唯一字段时,@ Gedmo\SoftDeleteable遇到问题。
如果我从桌子和皮革上移走一排,尝试放入新记录,并用这个名字给mi错误:

I have problem with @Gedmo\SoftDeleteable when i have unique fields. If I remove some row from table and leather a try put new record and by have this some name give mi error:


SQLSTATE [23000]:违反完整性约束:1062键'UNIQ _ *************'的重复条目'weqwewqe1'

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'weqwewqe1' for key 'UNIQ_*************'

如何在正确的位置将其更改为验证表单消息?

并且应该给我发送一个带有记录的消息表单

And should send me a message form with the record already exists.

我的实体:

    /**
 * @ORM\Entity(repositoryClass = "Eteam\PageBundle\Entity\Repository\PageRepository")
 * @ORM\Table(name = "page")
 * @ORM\HasLifecycleCallbacks
 * @Gedmo\SoftDeleteable(fieldName = "deletedDate", timeAware = false)
 *
 * @UniqueEntity(fields = {"name"})
 * @UniqueEntity(fields = {"slug"})
 */
class Page
{
    /**
     * @ORM\Column(name = "id", type = "integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy = "AUTO")
     */
    private $id;

    /**
     * @ORM\Column(name = "name", type = "string", length = 120, unique = true)
     *
     * @Assert\NotBlank
     * @Assert\Length(
     *      min = 3,
     *      max = 120
     * )
     */
    private $name;

    /**
     * @ORM\Column(name = "slug", type = "string", length = 120, unique = true)
     *
     * @Assert\NotBlank
     * @Assert\Length(
     *      min = 3,
     *      max = 120
     * )
     */
    private $slug;

    /**
     * @ORM\Column(name = "type", type = "string", length = 50)
     */
    private $type;

    /**
     * @ORM\Column(name = "status", type = "string", length = 50)
     */
    private $status;

    /**
     * @ORM\OneToMany(targetEntity = "PageContent", mappedBy = "pageId")
     */
    private $content;

    /**
     * @ORM\Column(name = "parent", type = "string", length = 50, nullable = true)
     */
    private $parent;

    /**
     * @ORM\ManyToOne(targetEntity = "PageTemplate", inversedBy = "page")
     * @ORM\JoinColumn(name = "page_template_id", referencedColumnName = "id", onDelete = "SET NULL")
     */
    private $pageTemplate;

    /**
     * @ORM\ManyToOne(targetEntity = "PageContentMap", inversedBy = "page")
     * @ORM\JoinColumn(name = "page_content_map_id", referencedColumnName = "id", onDelete = "SET NULL")
     */
    private $pageContentMapId;

    /**
     * @ORM\Column(name = "created_date", type = "datetime")
     */
    private $createdDate;

    /**
     * @ORM\Column(name = "updated_date", type = "datetime", nullable = true)
     */
    private $updatedDate;

    /**
     * @var \DateTime deletedDate
     * @ORM\Column(name = "deleted_date", type = "datetime", nullable = true)
     */
    private $deletedDate;


    /**
     * Constructor
     */
    public function __construct()
    {
        $this->content = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Page
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set slug
     *
     * @param string $slug
     *
     * @return Page
     */
    public function setSlug($slug)
    {
        $genSlug = new Slugify();
        $this->slug = $genSlug->slugify($slug);

        return $this;
    }

    /**
     * Get slug
     *
     * @return string
     */
    public function getSlug()
    {
        return $this->slug;
    }

    /**
     * Set type
     *
     * @param string $type
     *
     * @return Page
     */
    public function setType($type)
    {
        $this->type = $type;

        return $this;
    }

    /**
     * Get type
     *
     * @return string
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * Set status
     *
     * @param string $status
     *
     * @return Page
     */
    public function setStatus($status)
    {
        $this->status = $status;

        return $this;
    }

    /**
     * Get status
     *
     * @return string
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * Set parent
     *
     * @param string $parent
     *
     * @return Page
     */
    public function setParent($parent)
    {
        $this->parent = $parent;

        return $this;
    }

    /**
     * Get parent
     *
     * @return string
     */
    public function getParent()
    {
        return $this->parent;
    }

    /**
     * Set createdDate
     *
     * @param \DateTime $createdDate
     *
     * @return Page
     */
    public function setCreatedDate($createdDate)
    {
        $this->createdDate = $createdDate;

        return $this;
    }

    /**
     * Get createdDate
     *
     * @return \DateTime
     */
    public function getCreatedDate()
    {
        return $this->createdDate;
    }

    /**
     * Set updatedDate
     *
     * @param \DateTime $updatedDate
     *
     * @return Page
     */
    public function setUpdatedDate($updatedDate)
    {
        $this->updatedDate = $updatedDate;

        return $this;
    }

    /**
     * Get updatedDate
     *
     * @return \DateTime
     */
    public function getUpdatedDate()
    {
        return $this->updatedDate;
    }

    /**
     * Set deletedDate
     *
     * @param \DateTime $deletedDate
     *
     * @return Page
     */
    public function setDeletedDate($deletedDate)
    {
        $this->deletedDate = $deletedDate;

        return $this;
    }

    /**
     * Get deletedDate
     *
     * @return \DateTime
     */
    public function getDeletedDate()
    {
        return $this->deletedDate;
    }

    /**
     * Add content
     *
     * @param \Eteam\PageBundle\Entity\PageContent $content
     *
     * @return Page
     */
    public function addContent(\Eteam\PageBundle\Entity\PageContent $content)
    {
        $this->content[] = $content;

        return $this;
    }

    /**
     * Remove content
     *
     * @param \Eteam\PageBundle\Entity\PageContent $content
     */
    public function removeContent(\Eteam\PageBundle\Entity\PageContent $content)
    {
        $this->content->removeElement($content);
    }

    /**
     * Get content
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getContent()
    {
        return $this->content;
    }

    /**
     * Set pageTemplate
     *
     * @param \Eteam\PageBundle\Entity\PageTemplate $pageTemplate
     *
     * @return Page
     */
    public function setPageTemplate(\Eteam\PageBundle\Entity\PageTemplate $pageTemplate = null)
    {
        $this->pageTemplate = $pageTemplate;

        return $this;
    }

    /**
     * Get pageTemplate
     *
     * @return \Eteam\PageBundle\Entity\PageTemplate
     */
    public function getPageTemplate()
    {
        return $this->pageTemplate;
    }

    /**
     * Set pageContentMapId
     *
     * @param \Eteam\PageBundle\Entity\PageContentMap $pageContentMapId
     *
     * @return Page
     */
    public function setPageContentMapId(\Eteam\PageBundle\Entity\PageContentMap $pageContentMapId = null)
    {
        $this->pageContentMapId = $pageContentMapId;

        return $this;
    }

    /**
     * Get pageContentMapId
     *
     * @return \Eteam\PageBundle\Entity\PageContentMap
     */
    public function getPageContentMapId()
    {
        return $this->pageContentMapId;
    }

    /**
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function preSave()
    {
        if (null === $this->slug) {
            $this->setSlug($this->getName());
        }

        if (null == $this->status) {
            $this->setStatus('unpublish');
        }

    }






钢我尚未解决此问题。我需要捕获此异常并将此名称准备好发送给表单消息。
请帮助。

推荐答案

您知道软删除是什么意思吗?这意味着每个删除操作都将转换为仅将某些 deleted 标志设置为 true 的SQL。并且,如果您插入另一行具有相同的唯一字段值且已软删除行的行,则会收到此消息。

Do you know what means "soft delete"? It means that every "delete" operation will be converted to SQL that only set some deleted flag to true. And if you will insert another row with the same value of unique field that have soft deleted row you will get this message.

您有两种方法可以解决此问题:

You have two ways to solve this problem:


  • 使用两列创建唯一索引:原始唯一字段和已删除标志。然后,仅当您尝试添加仅具有未软删除的现有唯一字段值的行时,才会出现此错误。

  • 避免发生此违规:您应该排除添加以下行的可能性:在唯一字段中重复另一个。

  • Make your unique index with two columns: your original unique field and deleted flag. Then you will get this error only when you try to add row with the existing unique fields values only for not soft deleted.
  • Avoid making this violation: you should exclude the possibility to add row that duplicates another in unique fields.

第二个是最好的恕我直言。

The second one is the best approach IMHO.

这篇关于教义2 @ Gedmo\Soft可删除和唯一的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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