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


i've got following models:

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; } 

user:

<?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; } 

and following controller code:

$employee = new \base\user(); $employee->username = "employee"; $employee->password = "just 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(); 

the controller code gives me:

pdoexception [ 23000 ]: sqlstate[23000]: integrity constraint violation: 1062  duplicate entry 'to authenticated' key 'name' 

needless say, name of loaded userstatus "to authenticated". somehow tries insert userstatus database instead of persisting onetomany relation of userstatus.

can tell me wrong code?

@beberlei: when remove line, different error:

invalidargumentexception [ 0 ]: new entity found through relationship not configured cascade persist operations: base\userstatus@00000000485b93d40000000031ebec33. explicitly persist new entity or  configure cascading persist operations on relationship. 

you calling $em->persist($userstatus) entity managed. method new entities. not sure if solves issue though.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -