DDC-3221: Invalid binding for primary key of entity relation #3988

Closed
opened 2026-01-22 14:33:03 +01:00 by admin · 1 comment
Owner

Originally created by @doctrinebot on GitHub (Jul 21, 2014).

Originally assigned to: @ostrolucky on GitHub.

Jira issue originally created by user to0n1:

Hello.

We use doctrine2 ORM with doctrine/doctrine-bundle in our symfony2 based project. We developed functionality which is similar to some kind of import process.

And we have an issue that appears from time to time in different points during the import.

Issue is following:
Lets imagine we have 3 entities Account, Contact, Contact Address.
Account has many to one relation on contact and contact has one to many relation on contact address.

Our import creates all 3 entities and persist only Account, contact and address are persisted via cascade persist.
We have writer that contains following code

    public function write(array $items)
    {
        try {
            $this->entityManager->beginTransaction();
            foreach ($items as $item) {
                $this->entityManager->persist($item);
            }
            $this->entityManager->commit();
        } catch (\Exception $exception) {
            $this->entityManager->rollback();

            throw $exception;
        }
        $this->entityManager->flush();
        $this->entityManager->clear();
    }

So we clear EntityManager for each batch in order to avoid high memory consumption.
Import fails during different entities insert, but errors are very similar.
Example of error is
{quote}

[error] An exception occurred while executing 'INSERT INTO ... VALUES (?, ?, ?, ?, ?)' with params ["start", "2014-07-21 00:55:35", 0, null, 21]:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (DB.account, CONSTRAINT FK*B3D57B301023C4EE FOREIGN KEY (contact*id) REFERENCES contact (id) ON DELETE CASCADE)
{quote}

After debugging I found that problem is in BasicEntityPersister#prepareUpdateData $uow->getEntityIdentifier($newVal);
returns oid that is real one, but UOW contains not the same ID as in $newVal entity. It seems like spl_object_hash duplicates oid.

Any help is appreciated.
Thanks in advance.

Originally created by @doctrinebot on GitHub (Jul 21, 2014). Originally assigned to: @ostrolucky on GitHub. Jira issue originally created by user to0n1: Hello. We use doctrine2 ORM with doctrine/doctrine-bundle in our symfony2 based project. We developed functionality which is similar to some kind of import process. And we have an issue that appears from time to time in different points during the import. Issue is following: Lets imagine we have 3 entities Account, Contact, Contact Address. Account has many to one relation on contact and contact has one to many relation on contact address. Our import creates all 3 entities and persist only Account, contact and address are persisted via cascade persist. We have writer that contains following code ``` public function write(array $items) { try { $this->entityManager->beginTransaction(); foreach ($items as $item) { $this->entityManager->persist($item); } $this->entityManager->commit(); } catch (\Exception $exception) { $this->entityManager->rollback(); throw $exception; } $this->entityManager->flush(); $this->entityManager->clear(); } ``` So we clear EntityManager for each batch in order to avoid high memory consumption. Import fails during different entities insert, but errors are very similar. Example of error is {quote} > [error] An exception occurred while executing 'INSERT INTO ... VALUES (?, ?, ?, ?, ?)' with params ["__start__", "2014-07-21 00:55:35", 0, null, 21]: > > SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`DB`.`account`, CONSTRAINT `FK*B3D57B301023C4EE` FOREIGN KEY (`contact*id`) REFERENCES `contact` (`id`) ON DELETE CASCADE) > {quote} After debugging I found that problem is in BasicEntityPersister#prepareUpdateData `$uow->getEntityIdentifier($newVal);` returns oid that is real one, but UOW contains not the same ID as in $newVal entity. It seems like spl_object_hash duplicates oid. Any help is appreciated. Thanks in advance.
admin added the Bug label 2026-01-22 14:33:03 +01:00
admin closed this issue 2026-01-22 14:33:04 +01:00
Author
Owner

@ostrolucky commented on GitHub (Aug 7, 2018):

spl_object_has has been replaced with spl_object_id in doctrine 3.x, so this should no longer happen

@ostrolucky commented on GitHub (Aug 7, 2018): spl_object_has has been replaced with spl_object_id in doctrine 3.x, so this should no longer happen
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3988