I've got the 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 the following controller code:
$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();
The controller code gives me:
PDOException [ 23000 ]: SQLSTATE[23000]: Integrity constraint violation: 1062
Duplicate entry 'To be authenticated' for key 'name'
Needless to say, but the name of the loaded UserStatus is "To be authenticated". Somehow it tries to insert the UserStatus into the database instead of persisting the OneToMany relation of the UserStatus.
Can somebody tell me what is wrong with this code?
@beberlei: When I remove开发者_运维技巧 that line, I get a different error:
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.
You are calling $em->persist($userStatus) for an entity that is already managed. This method is only for new entities. I am not sure if this solves the issue though.
I found the solution. My $entityManager was not a singleton. Which means that the $UserStatus
was loaded by an other instance of the entitymanager then the one I tried to save it with. The one I tried to save it with did not know $UserStatus yet, so it tried to insert it. By making my \Doctrine\Configure\EntityManager::instance()
return a singleton instance of the entitymanager the problem was solved.
精彩评论