Symfony原则违反“多对多诚信”约束:1062重复输入 [英] Symfony doctrine Many to Many Integrity constraint violation: 1062 Duplicate entry

查看:81
本文介绍了Symfony原则违反“多对多诚信”约束:1062重复输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有manyToMany(自引用)关系的实体用户。

I have an entity User with a manyToMany ('Self-Referencing') relation.

/**
     * @Serializer\Expose()
     * @ORM\ManyToMany(targetEntity="User")
     * @ORM\JoinTable(name="user_referent",
     *  joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *  inverseJoinColumns={@ORM\JoinColumn(name="coach_id", referencedColumnName="id")}
     * )
     * @ORM\JoinColumn(nullable=true)
     * @Assert\Valid
     *
     */
    private $coaches;

在测试中,当我尝试创建重复条目时,我从sql得到了正确的消息( SQLSTATE [23000]:违反完整性约束:1062重复的条目。
但是我想在刷新之前捕获重复的内容。因此,我在add函数中添加了if语句。

During my test when I try to create a duplicate entry, I have the right message from sql (SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry.) But I would like to catch the duplicate before flushing. So I added a if statement in the add function.

public function addCoach(User $coach)
    {
        if ($this->coaches->contains($coach)) {
            return;
        }
        $this->coaches[] = $coach;

    }

但是似乎在使用表单时,addCoach函数不被调用。当我转储它时,没有值被转储。
我尝试了 @ Assert\Valid @Unique @Table ...但没有任何效果。
是否有任何方法可以设置与多对多关系的约束,从而像在实体的任何其他项目上一样抛出消息?

But it seems that when using a form, the addCoach function is not called. When I dump it, no value is dumped. I tried the @Assert\Valid, the @Unique or constraint on @Table... But nothing works. Is there any way to set a constraint the many to many relation a throw an message like on any other item of an entity?

推荐答案

仅当您在持久化持久对象之前显式调用该方法以在用户实体上添加教练时,才会调用addCoach方法。不会被隐式调用。

The addCoach method will be called only when you call that method explicitly to add coach on user entity before persisting it. It does not get called implicitly.

您可以在User实体中使用onPrePersist方法,这是一种隐式方式,每次您尝试将实体刷新到DB中时都检查重复项。

You can use onPrePersist method in User entity which will be implicit way to check for duplicates every time you try to flush an entity into DB.

使用以下内容更新用户实体的准则orm文件。

Update your User entity's doctrine orm file with following.

<lifecycle-callbacks>
   <lifecycle-callback type="prePersist" method="prePersist" />
</lifecycle-callbacks>

然后使用以下命令更新用户实体文件。

And then update User entity file with following.

    /**
     * @PrePersist
     */
    public function onPrePersist()
    {
        // remove duplicates from coaches array.

        $coachKeys[] = array();
        foreach($this->coaches as $key => $coach) {
            if(!in_array($coach->getUserId(), $coachKeys) {
                $coachKeys[] = $key;
            } else {
               $this->removeCoach($this->coach); // unset($this->coache[$key]);
            }     
        }           
    }

这将确保删除重复项在抽水之前。

This will make sure it removes duplicate entry before the flush.

这篇关于Symfony原则违反“多对多诚信”约束:1062重复输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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