Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property promotion not working correctly with associations/proxies #11077

Open
delolmo opened this issue Nov 20, 2023 · 1 comment
Open

Property promotion not working correctly with associations/proxies #11077

delolmo opened this issue Nov 20, 2023 · 1 comment

Comments

@delolmo
Copy link

delolmo commented Nov 20, 2023

Bug Report

Q A
BC Break no
Version 2.17.1

Summary

I opened this #10456 in the past, hoping that it had been fixed in #10385, but I still keep getting the same error.

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.

@delolmo
Copy link
Author

delolmo commented Nov 23, 2023

Just to continue with the example, this actually works (although it comes as no surprise):

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant