[英]Error when persisting doctrine entity with OneToMany association
New entities in a collection using cascade persist will produce an Exception and rollback the flush() operation. 使用级联持久化的集合中的新实体将产生异常并回滚flush()操作。 The reason is that the "UserGroupPrivilege" entity has identity through a foreign entity "UserGroup".
原因是“ UserGroupPrivilege”实体通过外部实体“ UserGroup”具有身份。
But if the "UserGroupPrivilege" has its own identity with auto generated value the code works just fine, and I don't want that I want the identity to be a composite key to enforce validation. 但是,如果“ UserGroupPrivilege”具有自己的身份以及自动生成的值,则代码可以正常工作,并且我不希望我将身份用作执行验证的复合键。 here is my code:
这是我的代码:
Entity UserGroup: 实体用户组:
class UserGroup
{
/**
* @var integer
*
* @ORM\Column(type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var integer
*
* @ORM\Column(type="boolean", nullable=false)
* @Type("integer")
*/
private $active;
/**
* @ORM\OneToMany(targetEntity="UserGroupPrivilege", mappedBy="userGroup", cascade={"persist"})
*/
private $privileges;
Entity UserGroupPrivilege: 实体UserGroupPrivilege:
class UserGroupPrivilege
{
/**
* @var integer
*
* @ORM\Id
* @ORM\Column(type="integer", nullable=false)
*
*/
private $privilegeId;
/**
* @var UserGroup
*
* @ORM\Id
* @ORM\ManyToOne(targetEntity="UserGroup", inversedBy="privileges")
* @ORM\JoinColumn(name="userGroupId", referencedColumnName="id")
*/
private $userGroup;
/**
* @var string
* @ORM\Column(type="string", nullable=false)
*/
private $name;
/**
* @var string
* @ORM\Column(type="string", nullable=false)
*/
private $value;
Controller: 控制器:
$userGroup = new UserGroup();
$userGroupPrivilege = new UserGroupPrivilege();
userGroupPrivilege->setUserGroup($userGroup)
->setName($arrPrivilege['name'])
->setValue($arrPrivilege['value'])
->setPrivilegeId($arrPrivilege['privilegeId']);
$userGroup->addPrivilege($userGroupPrivilege);
$data = $repo->saveUserGroup($userGroup);
return $data;
Repository: 仓库:
$em = $this->getEntityManager();
$em->persist($userGroup);
$em->flush();
I get the following error: 我收到以下错误:
Entity of type UserGroupPrivilege has identity through a foreign entity UserGroup, however this entity has no identity itself. You have to call EntityManager#persist() on the related entity and make sure that an identifier was generated before trying to persist 'UserGroupPrivilege'. In case of Post Insert ID Generation (such as MySQL Auto-Increment or PostgreSQL SERIAL) this means you have to call EntityManager#flush() between both persist operations.
Error message is pretty self explanatory. 错误消息很容易解释。 To relate
UserGroupPrivilege
to UserGroup
, UserGroup
must have it's ID set. 要涉及
UserGroupPrivilege
到UserGroup
, UserGroup
必须有它的ID集。 However, since you've just created both entities it has no id because it hasn't been persisted to database yet. 但是,由于您刚刚创建了两个实体,因此它没有ID,因为它尚未持久化到数据库中。
In your case : 在您的情况下:
$em = $this->getEntityManager();
$em->persist($userGroup);
$em->persist($userGroupPrivilege);
$em->flush();
Can you "enforce validation" with unique constraint: 您可以通过唯一的约束来“强制执行验证”:
/**
* @Entity
* @Table(uniqueConstraints={@UniqueConstraint(name="ugppriv_idx", columns={"priviledgeId", "userGroup"})})
*/
class UserGroupPriviledge
{
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.