Uploaded image for project: 'Doctrine 2 - ORM'
  1. Doctrine 2 - ORM
  2. DDC-2965

Error after changing IdGenerator to AssignedGenerator when for this entity class insert has been already executed

    Details

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

      Description

      When you persist and flush entity and then change id generator to AssignedGenerator (and id generator type to NONE) for this entity class and then you persist and flush new entity with assigned id, below error will occur:

      Exception: [Doctrine\DBAL\Exception\DriverException] An exception occurred while executing 'INSERT INTO cms_emails (email) VALUES ' with params [13, "example@example.com"]:

      SQLSTATE[HY000]: General error: 25 bind or column index out of range

      There is test that reproduces this issue:

          public function testPersistEntityAndThenSwitchToAssignedIdGenerator()
          {
              $email = new \Doctrine\Tests\Models\CMS\CmsEmail();
              $email->email = 'example5@example.com';
      
              $this->_em->persist($email);
              $this->_em->flush();
      
              $this->assertNotNull($email->id);
      
              $classMetadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsEmail');
              $classMetadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_NONE);
              $classMetadata->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator());
      
              $id = 13;
      
              $newEmail = new \Doctrine\Tests\Models\CMS\CmsEmail();
              $newEmail->email = 'example@example.com';
              $newEmail->id = $id;
      
              $this->_em->persist($newEmail);
              $this->_em->flush();
              $this->_em->clear();
      
              $newEmail = $this->_em->find('Doctrine\Tests\Models\CMS\CmsEmail', $id);
      
              $this->assertNotNull($newEmail);
          }
      

      The problem is in BasicEntityPersister because insertSql is cached and is not cleared after changing id generator. Binded parameters count doesn't match with insert query because there is new parameter: id.

        Issue Links

          Activity

          Hide
          ocramius Marco Pivetta added a comment -
          Show
          ocramius Marco Pivetta added a comment - Provided a possible fix at https://github.com/doctrine/doctrine2/pull/942
          Hide
          doctrinebot Doctrine Bot added a comment -

          A related Github Pull-Request [GH-942] was closed:
          https://github.com/doctrine/doctrine2/pull/942

          Show
          doctrinebot Doctrine Bot added a comment - A related Github Pull-Request [GH-942] was closed: https://github.com/doctrine/doctrine2/pull/942
          Hide
          beberlei Benjamin Eberlei added a comment -

          It is not a supported use case to change metadata after loading them.

          Show
          beberlei Benjamin Eberlei added a comment - It is not a supported use case to change metadata after loading them.

            People

            • Assignee:
              ocramius Marco Pivetta
              Reporter:
              psliwa Piotr Śliwa
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: