Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-1315

ORM\Id\AssignedGenerator doesn't work with cascade persist and auto incremented id

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Duplicate
    • Affects Version/s: 3.0
    • Fix Version/s: None
    • Component/s: None
    • Security Level: All
    • Labels:
      None

      Description

                              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.

        Issue Links

          Activity

          Hide
          Benjamin Eberlei added a comment -

          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.

          Show
          Benjamin Eberlei added a comment - 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.
          Hide
          Benjamin Eberlei added a comment -

          closed, duplicate of DDC-1200

          Show
          Benjamin Eberlei added a comment - closed, duplicate of DDC-1200

            People

            • Assignee:
              Benjamin Eberlei
              Reporter:
              Konstantin
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: