Doctrine2:PDOException [23000]:SQLSTATE [23000]:完整性约束违规:1062重复条目“要进行身份验证”以获取密钥“名称”

时间:2011-02-18 20:53:45

标签: php orm pdo doctrine-orm

我有以下型号:

UserStatus:

<?php
namespace Base;

/** @Entity @Table(name="user_status") */
class UserStatus extends \Skeleton\Base {
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /** @Column(length="256") */
    protected $name;

    /**
     * @OneToMany(targetEntity="Base\User", mappedBy="Status")
     */
    protected $Users;
}

用户:

<?php
namespace Base;

/**
 * @Entity 
 * @Table(name="user")
 * @InheritanceType("JOINED")
 * @DiscriminatorColumn(name="discriminator", type="string")
 * @DiscriminatorMap({
 *          "admin"         = "Administrator", 
 *          "participant"   = "Participant", 
 *          "employee"      = "Employee" 
 * })
 */
class User extends \Skeleton\Skeleton implements Interfaces\User {
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ManyToOne(targetEntity="UserStatus", inversedBy="Users")
     * @JoinColumn(name="status_id", referencedColumnName="id")
     */
    protected $Status;

    /** @Column(length=255) */
    protected $username;

    /** @Column(length=255) */
    protected $password;

    /** @Column(length=255) */
    protected $firstname;

    /** @Column(length=128) */
    protected $insertion;

    /** @Column(length=255) */
    protected $lastname;

    /** @Column(type="datetime") */
    protected $created;

    /** @Column(type="integer", name="creator_id") */
    protected $Creator;

    /** @Column(type="datetime") */
    protected $modified;

    /** @Column(type="integer", name="modifier_id") */
    protected $Modifier;
}

以下控制器代码:

$Employee = new \Base\User();
$Employee->username = "Employee";
$Employee->password = "Just an encrypted password";
$Employee->firstname = "Just";
$Employee->insertion = "a";
$Employee->lastname = "Employee";

$UserStatus = \Base\UserStatus::getById(1);
$Employee->Status = $UserStatus;

$UserStatus->Users->add($Employee);

$em = \Doctrine\Configure\EntityManager::instance();
$em->persist($UserStatus);
$em->persist($Employee);
$em->flush();

控制器代码给了我:

PDOException [ 23000 ]: SQLSTATE[23000]: Integrity constraint violation: 1062 
Duplicate entry 'To be authenticated' for key 'name'

毋庸置疑,但加载的UserStatus的名称是“要进行身份验证”。不知何故,它试图将UserStatus插入到数据库中,而不是持久化UserStatus的OneToMany关系。

有人可以告诉我这段代码有什么问题吗?

@beberlei:当我删除该行时,我得到一个不同的错误:

InvalidArgumentException [ 0 ]: A new entity was found through a relationship that was
not configured to cascade persist operations: Base\UserStatus@00000000485b93d40000000031ebec33. Explicitly persist the new entity or 
configure cascading persist operations on the relationship.

2 个答案:

答案 0 :(得分:1)

您正在为已管理的实体调用$ em-&gt; persist($ userStatus)。此方法仅适用于新实体。我不确定这是否能解决这个问题。

答案 1 :(得分:1)

我找到了解决方案。我的$ entityManager不是单身人士。这意味着$UserStatus由实体管理器的另一个实例加载,然后是我试图保存的实例。我试图保存它的那个还不知道$ UserStatus,所以它试图插入它。通过使我的\Doctrine\Configure\EntityManager::instance()返回实体管理器的单例实例,问题得以解决。