SingleTable inherited entity collection is accessed without being initialized #7132

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

Originally created by @pkly on GitHub (Apr 13, 2023).

Bug Report

Q A
BC Break unsure
Version 2.14.2 and earlier

Summary

An abstract entity with a setup like so

#[ORM\Entity()]
#[ORM\InheritanceType('SINGLE_TABLE')]
abstract class BaseFile
{
#[ORM\ManyToOne(inversedBy: 'childrenFiles')]
#[ORM\JoinColumn(nullable: true, onDelete: 'CASCADE')]
private ?BaseFile $parentFile = null;

/** @var Collection<int, BaseFile> */
#[ORM\OneToMany(mappedBy: 'parentFile', targetEntity: BaseFile::class)]
private Collection $childrenFiles;

public function __construct()
{
  $this->childrenFiles = new ArrayCollection();
}

// getters / setters
}

Using typed properties throws an error when calling it's getter, getChildrenFiles(): Collection which simply returns the $childrenFiles property.

This behavior does not occur when not using typed properties in an older version of Doctrine, on php7.4 (currently in the process of upgrading)

Current behavior

BaseFile::$childrenFiles must not be accessed before initialization

Error message is shown when attempting to call getChildrenFiles which returns a Collection<int, self>

How to reproduce

Use the class above along with something like this

#[ORM\Entity()]
class SubFile extends BaseFile
{
}

then load the entity SubFile (any existing, valid) and call $subFile->getChildrenFiles(), for some reason doctrine does not automatically hydrate the property with an empty collection, nor is the constructor called.

Expected behavior

An empty collection is returned by getChildrenFiles getter in BaseFile

Originally created by @pkly on GitHub (Apr 13, 2023). ### Bug Report | Q | A |------------ | ------ | BC Break | unsure | Version | 2.14.2 and earlier #### Summary An abstract entity with a setup like so ```php #[ORM\Entity()] #[ORM\InheritanceType('SINGLE_TABLE')] abstract class BaseFile { #[ORM\ManyToOne(inversedBy: 'childrenFiles')] #[ORM\JoinColumn(nullable: true, onDelete: 'CASCADE')] private ?BaseFile $parentFile = null; /** @var Collection<int, BaseFile> */ #[ORM\OneToMany(mappedBy: 'parentFile', targetEntity: BaseFile::class)] private Collection $childrenFiles; public function __construct() { $this->childrenFiles = new ArrayCollection(); } // getters / setters } ``` Using typed properties throws an error when calling it's getter, `getChildrenFiles(): Collection` which simply returns the `$childrenFiles` property. This behavior does not occur when not using typed properties in an older version of Doctrine, on php7.4 (currently in the process of upgrading) #### Current behavior ``` BaseFile::$childrenFiles must not be accessed before initialization ``` Error message is shown when attempting to call `getChildrenFiles` which returns a Collection<int, self> #### How to reproduce Use the class above along with something like this ```php #[ORM\Entity()] class SubFile extends BaseFile { } ``` then load the entity SubFile (any existing, valid) and call $subFile->getChildrenFiles(), for some reason doctrine does not automatically hydrate the property with an empty collection, nor is the constructor called. #### Expected behavior An empty collection is returned by `getChildrenFiles` getter in `BaseFile`
admin closed this issue 2026-01-22 15:45:22 +01:00
Author
Owner

@pkly commented on GitHub (Apr 13, 2023):

I'm sorry, it seems that I was mistaken and instead it was an issue with me loading some info about an object when it was read back from cache.

@pkly commented on GitHub (Apr 13, 2023): I'm sorry, it seems that I was mistaken and instead it was an issue with me loading some info about an object when it was read back from cache.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7132