简体   繁体   中英

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. The reason is that the "UserGroupPrivilege" entity has identity through a foreign entity "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. 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:

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. However, since you've just created both entities it has no id because it hasn't been persisted to database yet.

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
{
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM