2011-02-18 118 views
1

'被認證' 我已經得到了以下型號:Doctrine2:PDOException [23000]:SQLSTATE [23000]:完整性約束違規:1062重複條目鍵 '名'

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\[email protected] Explicitly persist the new entity or 
configure cascading persist operations on the relationship. 

回答

1

我找到了解決方案。我的$ entityManager不是一個單身人士。這意味着$UserStatus由entitymanager的另一個實例加載,然後我試圖將其保存。我試圖保存它的那個人並不知道$ UserStatus,所以它試圖插入它。通過讓我的\Doctrine\Configure\EntityManager::instance()返回entitymanager的單例實例,問題就解決了。

+0

Tnx分享。恕我直言,單身人士總是有風險的野獸(http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/)。而依賴注入可以幫助解決類似的情況,而不會引入單例造成的問題。只有2cc。 – maraspin 2012-08-11 14:17:59

1

要調用EM-$>堅持($ userStatus)爲已管理的實體。此方法僅適用於新實體。我不確定這是否能解決問題。

+0

請檢查我的編輯。 – 2011-02-18 22:34:08

相關問題