DDC-2965: Error after changing IdGenerator to AssignedGenerator when for this entity class insert has been already executed #3692

Closed
opened 2026-01-22 14:25:31 +01:00 by admin · 5 comments
Owner

Originally created by @doctrinebot on GitHub (Feb 8, 2014).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user psliwa:

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:

{quote}
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
{quote}

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.

Originally created by @doctrinebot on GitHub (Feb 8, 2014). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user psliwa: 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: {quote} 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 {quote} 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**.
admin added the Bug label 2026-01-22 14:25:31 +01:00
admin closed this issue 2026-01-22 14:25:34 +01:00
Author
Owner

@doctrinebot commented on GitHub (Feb 8, 2014):

@doctrinebot commented on GitHub (Feb 8, 2014): - is referenced by [DDC-2966: [GH-942] DDC-2965 - Changing ID generation after `flush` makes persisters unusable](http://www.doctrine-project.org/jira/browse/DDC-2966)
Author
Owner

@doctrinebot commented on GitHub (Feb 9, 2014):

Comment created by @ocramius:

Provided a possible fix at https://github.com/doctrine/doctrine2/pull/942

@doctrinebot commented on GitHub (Feb 9, 2014): Comment created by @ocramius: Provided a possible fix at https://github.com/doctrine/doctrine2/pull/942
Author
Owner

@doctrinebot commented on GitHub (Feb 9, 2014):

Comment created by @doctrinebot:

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

@doctrinebot commented on GitHub (Feb 9, 2014): Comment created by @doctrinebot: A related Github Pull-Request [GH-942] was closed: https://github.com/doctrine/doctrine2/pull/942
Author
Owner

@doctrinebot commented on GitHub (Feb 9, 2014):

Comment created by @beberlei:

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

@doctrinebot commented on GitHub (Feb 9, 2014): Comment created by @beberlei: It is not a supported use case to change metadata after loading them.
Author
Owner

@doctrinebot commented on GitHub (Feb 9, 2014):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Feb 9, 2014): Issue was closed with resolution "Invalid"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3692