Property promotion not working correctly with associations/proxies #7096

Closed
opened 2026-01-22 15:44:37 +01:00 by admin · 3 comments
Owner

Originally created by @delolmo on GitHub (Jan 25, 2023).

Bug Report

Q A
BC Break no
Version 2.14.1

Summary

Given that associations are loaded through proxies, when using constructor promotion in entities, PHP will throw an error when trying to access a property of the proxy object.

Current behavior

The stated above.

How to reproduce

Given these entities (i.e.):


use Doctrine\ORM\Mapping as ORM;
use Ramsey\UuidInterface;

final class User 
{
      public function __construct(
            #[ORM\Id]
            #[ORM\Column(type: 'uuid_binary')]
            public UuidInterface $id,
            #[ORM\Column(type: 'string', unique: true)
            public string $username,
      ){
      }
}

final class Group
{
      public function __construct(
            #[ORM\Id]
            #[ORM\Column(type: 'uuid_binary')]
            public UuidInterface $id,
            #[ORM\Column(type: 'string', unique: true)
            public string $name,
      ){
      }
}

final readonly class Member
{
      public function __construct(
            #[ORM\Id]
            #[ORM\Column(type: 'uuid_binary')]
            public UuidInterface $id,
            #[ORM\ManyToOne(targetEntity: User::class)
            public User $user,
            #[ORM\ManyToOne(targetEntity: Group::class)
            public Group $group,
      ){
      }
}

One would expect this code to work:

$membersRepo = $entityManager->getRepository(Member::class);
$allMembers = $membersRepo->findAll();

foreach ($allMembers as $member) {
        print_r([$member->user->username, $member->group->name]);
}

But the following error is thrown:

PHP Fatal error:  Uncaught Error: Typed property User::$username must not be accessed before initialization in (...)

Expected behavior

If possible, no error should be thrown and properties should be initialized when trying to access the proxy object.

Originally created by @delolmo on GitHub (Jan 25, 2023). ### Bug Report <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | BC Break | no | Version | 2.14.1 #### Summary Given that associations are loaded through proxies, when using constructor promotion in entities, PHP will throw an error when trying to access a property of the proxy object. #### Current behavior The stated above. #### How to reproduce Given these entities (i.e.): ```php use Doctrine\ORM\Mapping as ORM; use Ramsey\UuidInterface; final class User { public function __construct( #[ORM\Id] #[ORM\Column(type: 'uuid_binary')] public UuidInterface $id, #[ORM\Column(type: 'string', unique: true) public string $username, ){ } } final class Group { public function __construct( #[ORM\Id] #[ORM\Column(type: 'uuid_binary')] public UuidInterface $id, #[ORM\Column(type: 'string', unique: true) public string $name, ){ } } final readonly class Member { public function __construct( #[ORM\Id] #[ORM\Column(type: 'uuid_binary')] public UuidInterface $id, #[ORM\ManyToOne(targetEntity: User::class) public User $user, #[ORM\ManyToOne(targetEntity: Group::class) public Group $group, ){ } } ``` One would expect this code to work: ```php $membersRepo = $entityManager->getRepository(Member::class); $allMembers = $membersRepo->findAll(); foreach ($allMembers as $member) { print_r([$member->user->username, $member->group->name]); } ``` But the following error is thrown: ``` PHP Fatal error: Uncaught Error: Typed property User::$username must not be accessed before initialization in (...) ``` #### Expected behavior If possible, no error should be thrown and properties should be initialized when trying to access the proxy object.
admin closed this issue 2026-01-22 15:44:37 +01:00
Author
Owner

@mpdude commented on GitHub (Jan 26, 2023):

Is this related to #10385?

@mpdude commented on GitHub (Jan 26, 2023): Is this related to #10385?
Author
Owner

@delolmo commented on GitHub (Jan 27, 2023):

Seems like it, sorry! Closing the issue.

@delolmo commented on GitHub (Jan 27, 2023): Seems like it, sorry! Closing the issue.
Author
Owner

@delolmo commented on GitHub (Nov 20, 2023):

I have revisited this issue and I am still having the same problem, despite #10385 being supposedly fixed. I am currently working with doctrine 2.17.1.

@delolmo commented on GitHub (Nov 20, 2023): I have revisited this issue and I am still having the same problem, despite #10385 being supposedly fixed. I am currently working with doctrine `2.17.1`.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7096