Doctrine_Collection forgets it is part of a reference when serialized #6525

Closed
opened 2026-01-22 15:34:31 +01:00 by admin · 1 comment
Owner

Originally created by @spaanse on GitHub (Aug 26, 2020).

Short description:
A Doctrine_Collection does not remember it's _referenceField under serialisation. This means that under serialisation/unseralisation any new members don't get their reference updated.

Let there be two database tables Foo and Bar, where there is a one-to-many relation between them:

Foo:
  tableName: tblfoo
  columns:
    foo_id: {primary: true, type: integer(4), autoincrement: true}
  relations:
    Bar:
      local: foo_id
      foreign: bar_foo_id
Bar:
  tableName: tblbar
  columns:
    bar_id: {primary: true, type: integer(4), autoincrement: true}
    bar_foo_id: integer(4)
  relations:
    Foo:
      local: bar_foo_id
      foreign: foo_id

Then when the following code is run the output is null instead of a Foo object:

$foo = new Foo();
$cache = serialize($foo);
...
$foo = unserialize($cache)
$bar = new Bar();
$foo->Bar->add($bar);
return $bar->Foo;

This is because the Doctrine_Collection $foo->Bar has cleared it's _referenceField under serialisation. Then after being unserialised it is just a normal Doctrine_Collection, so does not automatically set $bar->Foo when adding the object.

Originally created by @spaanse on GitHub (Aug 26, 2020). Short description: A Doctrine_Collection does not remember it's `_referenceField` under serialisation. This means that under serialisation/unseralisation any new members don't get their reference updated. Let there be two database tables `Foo` and `Bar`, where there is a one-to-many relation between them: ```yml Foo: tableName: tblfoo columns: foo_id: {primary: true, type: integer(4), autoincrement: true} relations: Bar: local: foo_id foreign: bar_foo_id Bar: tableName: tblbar columns: bar_id: {primary: true, type: integer(4), autoincrement: true} bar_foo_id: integer(4) relations: Foo: local: bar_foo_id foreign: foo_id ``` Then when the following code is run the output is null instead of a Foo object: ```php $foo = new Foo(); $cache = serialize($foo); ... $foo = unserialize($cache) $bar = new Bar(); $foo->Bar->add($bar); return $bar->Foo; ``` This is because the Doctrine_Collection $foo->Bar has cleared it's `_referenceField` under serialisation. Then after being unserialised it is just a normal Doctrine_Collection, so does not automatically set $bar->Foo when adding the object.
admin closed this issue 2026-01-22 15:34:31 +01:00
Author
Owner

@spaanse commented on GitHub (Aug 26, 2020):

Apparantly we use an old version of Doctrine, so this might not be a problem anymore

@spaanse commented on GitHub (Aug 26, 2020): Apparantly we use an old version of Doctrine, so this might not be a problem anymore
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6525