【问题标题】:Doctrine 2 - On duplicate key update教义 2 - 重复密钥更新
【发布时间】:2015-02-22 20:54:10
【问题描述】:

我有一个使用多对多连接表与角色实体相关联的用户实体。

我的问题是我什么时候来更新它。

它会抛出以下错误:

An exception occurred while executing 'INSERT INTO user_role (user_id, role_id) VALUES (?, ?)' with params [38, 3]:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '38-3' for key 'PRIMARY'

现在,如果我正确理解这一点,问题是我正在尝试使用已经存在的主键插入另一行。

加入下面学说生成的表模式。

+---------+---------+------+-----+---------+-------+
| Field   | Type    | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| user_id | int(11) | NO   | PRI | NULL    |       |
| role_id | int(11) | NO   | PRI | NULL    |       |
+---------+---------+------+-----+---------+-------+

我的用户实体有一个方法 setRoles(),它将角色添加到 ArrayCollection,然后教义将尝试执行插入。

所以我想我想问教义中是否存在重复键方法?

或者有人可以给我一些关于如何实现这一点的指导,即使这意味着重新生成一个模式。

我是学说新手,我已按照初学者教程进行操作,但仍在仔细研究文档。

编辑

<?php
namespace Brs\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\Common\Collections\ArrayCollection;

class User implements UserInterface, \Serializable
{
    private $id;
    private $username;
    private $fname;
    private $lname;
    private $email;
    private $mobile;
    private $active;
    private $mentor;
    private $initialized;
    private $roles;
    private $password;
    private $tempPassword;

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

    public function getTempPassword(){
        return $this->tempPassword;
    }

    public function setTempPassword($tempPassword){
        $this->tempPassword = $tempPassword;

        return $this;
    }

    public function getUsername(){
        return $this->username;
    }

    public function getSalt(){
        return null;
    }

    public function getPassword(){
        return $this->password;
    }

    public function getRoles(){
        return $this->roles->toArray();
    }

    public function getRole(){
        return $this->roles;
    }

    public function setRoles($roles){
        $this->roles[] = $roles;
        return $this;
    }

    public function eraseCredentials(){

    }

    public function serialize(){
        return serialize(array(
            $this->id,
            $this->username,
            $this->password,
        ));
    }

    public function unserialize($serialized){
        list(
                $this->id,
                $this->username,
                $this->password,
            ) = unserialize($serialized);
    }

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

    /**
     * Set username
     *
     * @param string $username
     * @return User
     */
    public function setUsername($username)
    {
        $this->username = $username;

        return $this;
    }

    /**
     * Set fname
     *
     * @param string $fname
     * @return User
     */
    public function setFname($fname)
    {
        $this->fname = $fname;

        return $this;
    }

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

    /**
     * Set lname
     *
     * @param string $lname
     * @return User
     */
    public function setLname($lname)
    {
        $this->lname = $lname;

        return $this;
    }

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

    /**
     * Set email
     *
     * @param string $email
     * @return User
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

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

    /**
     * Set mobile
     *
     * @param string $mobile
     * @return User
     */
    public function setMobile($mobile)
    {
        $this->mobile = $mobile;

        return $this;
    }

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

    /**
     * Set active
     *
     * @param boolean $active
     * @return User
     */
    public function setActive($active)
    {
        $this->active = $active;

        return $this;
    }

    /**
     * Get active
     *
     * @return boolean 
     */
    public function getActive()
    {
        return $this->active;
    }

    /**
     * Set mentor
     *
     * @param boolean $mentor
     * @return User
     */
    public function setMentor($mentor)
    {
        $this->mentor = $mentor;

        return $this;
    }

    /**
     * Get mentor
     *
     * @return boolean 
     */
    public function getMentor()
    {
        return $this->mentor;
    }

    /**
     * Set initialized
     *
     * @param \DateTime $initialized
     * @return User
     */
    public function setInitialized($initialized)
    {
        $this->initialized = $initialized;

        return $this;
    }

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

    /**
     * Set password
     *
     * @param string $password
     * @return User
     */
    public function setPassword($password)
    {
        $this->password = $password;

        return $this;
    }
    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $targetEntity;

    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $inversedBy;


    /**
     * Add targetEntity
     *
     * @param \Brs\UserBundle\Entity\R $targetEntity
     * @return User
     */
    public function addTargetEntity(\Brs\UserBundle\Entity\R $targetEntity)
    {
        $this->targetEntity[] = $targetEntity;

        return $this;
    }

    /**
     * Remove targetEntity
     *
     * @param \Brs\UserBundle\Entity\R $targetEntity
     */
    public function removeTargetEntity(\Brs\UserBundle\Entity\R $targetEntity)
    {
        $this->targetEntity->removeElement($targetEntity);
    }

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

    /**
     * Add inversedBy
     *
     * @param \Brs\UserBundle\Entity\u $inversedBy
     * @return User
     */
    public function addInversedBy(\Brs\UserBundle\Entity\u $inversedBy)
    {
        $this->inversedBy[] = $inversedBy;

        return $this;
    }

    /**
     * Remove inversedBy
     *
     * @param \Brs\UserBundle\Entity\u $inversedBy
     */
    public function removeInversedBy(\Brs\UserBundle\Entity\u $inversedBy)
    {
        $this->inversedBy->removeElement($inversedBy);
    }

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

    /**
     * Add roles
     *
     * @param \Brs\UserBundle\Entity\Role $roles
     * @return User
     */
    public function addRole(\Brs\UserBundle\Entity\Role $roles)
    {
        $this->roles[] = $roles;

        return $this;
    }

    /**
     * Remove roles
     *
     * @param \Brs\UserBundle\Entity\Role $roles
     */
    public function removeRole(\Brs\UserBundle\Entity\Role $roles)
    {
        $this->roles->removeElement($roles);
    }
}

角色实体:

<?php
namespace Brs\UserBundle\Entity;

use Symfony\Component\Security\Core\Role\RoleInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

class Role implements RoleInterface{
    private $id;
    private $name;
    private $role;
    private $users;

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

    public function getRole(){
        return $this->role;
    }

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

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

        return $this;
    }

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

    /**
     * Set role
     *
     * @param string $role
     * @return Role
     */
    public function setRole($role)
    {
        $this->role = $role;

        return $this;
    }

    /**
     * Add users
     *
     * @param \Brs\UserBundle\Entity\User $users
     * @return Role
     */
    public function addUser(\Brs\UserBundle\Entity\User $users)
    {
        $this->users[] = $users;

        return $this;
    }

    /**
     * Remove users
     *
     * @param \Brs\UserBundle\Entity\User $users
     */
    public function removeUser(\Brs\UserBundle\Entity\User $users)
    {
        $this->users->removeElement($users);
    }

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

谢谢

亚当

【问题讨论】:

  • 这意味着您要再次为同一个用户设置角色(即相同的 role_id 和相同的 user_id) - 你为什么要这样做?此外,这是一个连接表,那么UPDATE 会采取什么行动?您应该向我们展示您的用户和角色实体。
  • 向我们展示导致 thsi 的代码
  • @prodigitalson 代码 sn-ps 按要求添加。如果连接确实存在,我将如何不设置连接?
  • @BartłomiejWach 代码 sn-ps 按要求添加。
  • 最好的方法是检查关系是否已经存在,因为 setRoles 执行新的插入查询

标签: php symfony doctrine-orm


【解决方案1】:

@Adam ArrayCollection 上有用于检查它的功能。前两个是containsindexOf - 它们在内部使用in_arrayarray_search,所以我认为您需要传入的角色与加载到集合中的身份映射中的实例相同- 因为它针对引用相等性进行测试。第三个,exists 允许你传入一个闭包作为比较函数。

我会在User 实体的hasRole 方法中使用这些方法之一:

public function hasRole(Role $role) {
    return $this->getRoles()->contains($role);
}

如果由于某种原因您需要使用exists,则可能如下所示:

public function hasRole(Role $role) {
    return $this->getRoles()->exists(function($key, $entity) use ($role) {
         return $entity->getId() === $role->getId();
    });
}

您当然可以更改该方法以比较更多字段或其他内容,或者允许您传入一组字段进行比较,但在这种情况下似乎不需要。

【讨论】:

    【解决方案2】:

    感谢您的所有意见 :-)。

    好吧,由于我缺乏理解,我不知道 mysql 复合键,感谢@prodigitalson 指出这一点。

    这是我在我的用户实体中实现的,用于检查组合键是否不存在,如果它们不存在则执行插入。 感谢 @kmlnvm 提供的指导。

    private $roleIds = array();
    
    public function setRoles($role){
        if($role !== null){
            foreach($this->getRoles() as $currentRole){
                $this->roleIds[] = $currentRole->getId();
            }
    
            if(!in_array($role->getId(),$this->roleIds)){
                $this->roles[] = $role;
            }
        }
    
        return $this;
    }
    

    我想输入我已实施的内容,这是一个好方法吗?

    谢谢

    亚当

    【讨论】:

    • 不需要自己循环,ArrayCollection 有方法。看我的回答。
    猜你喜欢
    • 1970-01-01
    • 2014-10-04
    • 2014-10-31
    • 2018-06-24
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    • 2011-09-30
    相关资源
    最近更新 更多