Skip to content

Loading…

DDC-1315: ORM\Id\AssignedGenerator doesn't work with cascade persist and auto incremented id #1929

Closed
doctrinebot opened this Issue · 4 comments

2 participants

@doctrinebot

Jira issue originally created by user realmfoo:

                        if (!$em->getUnitOfWork()->isInIdentityMap($value)) {
                            throw ORMException::entityMissingForeignAssignedId($entity, $value);
                        }

I have two associated entities:

class User {

    /****
     * @var int
     * @ORM\Id
     * @ORM\Column(type="integer", unique=true, nullable=false)
     * @ORM\GeneratedValue
     */
    protected $user_id;


    /****
     * @var \Doctrine\ORM\PersistentCollection
     * @ORM\OneToMany(targetEntity="Role", mappedBy="user", cascade={"persist", "remove", "merge"}, orphanRemoval=true)
     */
    protected $roles;
}

class Role {
    /****
     * @var User
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="User", inversedBy="roles")
     * @ORM\JoinColumn(name="user*id", referencedColumnName="user*id")
     */
    protected $user;

    ...
}

So WHY I can't use cascade persist when creating new user?

$Role = new Role();
$Role->setName("ROLE_SOME");
$User = new User();
$User->getRoles()->add($Role);
$em->persist($User);

So It worked before, but now results in:

Doctrine\ORM\ORMException: Entity of type Role has identity through a foreign entity User, however this entity has no ientity itself. You have to call EntityManager#persist() on the related entity and make sure it an identifier was generated before trying to persist 'Role'. 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.

Why should I flush User and only then flush Role?

Every flush runs within transaction, but I want to flush both entities within ONE transaction.

@doctrinebot

Comment created by @beberlei:

This couldn't have worked before, it would have lead to a php notice and unexpected behavior. This behavior is documented since the beginning of Foreign Keys of Primary Keys.

I know the requirement for two flushes is annoying, but we need to clean up some internals to make this possible.

@doctrinebot

Comment created by @beberlei:

closed, duplicate of DDC-1200

@doctrinebot

Issue was closed with resolution "Duplicate"

@beberlei beberlei was assigned by doctrinebot
@doctrinebot doctrinebot closed this
@doctrinebot doctrinebot added the Bug label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.