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

Public readonly properties are unset in Proxy's constructor #9432

Open
solcik opened this issue Jan 27, 2022 · 7 comments
Open

Public readonly properties are unset in Proxy's constructor #9432

solcik opened this issue Jan 27, 2022 · 7 comments

Comments

@solcik
Copy link

solcik commented Jan 27, 2022

Bug Report

Q A
BC Break no
Version 2.11.0

Summary

Public readonly properties are unset in Proxy's constructor, which results in error.

Current behavior

Cannot unset readonly property \Entity\SimpleBook::$title from scope Proxies\__CG__\Entity\SimpleBook

How to reproduce

#[Entity, Table]
class SimpleBook
{
    #[Column, Id, GeneratedValue]
    private readonly int $id;

    #[Column]
    public readonly string $title;

    #[ManyToOne, JoinColumn(nullable: false)]
    private readonly Author $author;

    public function getId(): int
    {
        return $this->id;
    }

    public function getTitle(): string
    {
        return $this->title;
    }

    public function getAuthor(): Author
    {
        return $this->author;
    }
}

class SimpleBook extends \Entity\SimpleBook implements \Doctrine\ORM\Proxy\Proxy
{
    // ...

    public function __construct(?\Closure $initializer = null, ?\Closure $cloner = null)
    {
        unset($this->title);

        $this->__initializer__ = $initializer;
        $this->__cloner__      = $cloner;
    }
}

Expected behavior

Code that generates Proxy's constructor

I dug into ProxyGenerator and here is generation of these unsets:

public readonly properties are not tested as I see in tests.

@acelaya
Copy link

acelaya commented Jan 15, 2023

I'm experiencing the same. Are readonly properties even supported in entities? 🤔

@kiler129
Copy link
Contributor

kiler129 commented Jan 15, 2023

Are readonly properties even supported in entities? 🤔

According to the docs at https://www.doctrine-project.org/projects/doctrine-orm/en/2.14/reference/architecture.html#entities - yes, as it says (emphasis mine):

An entity class must not be final nor read-only but it may contain final methods or read-only properties.

@acelaya
Copy link

acelaya commented Jan 15, 2023

Apparently the problem is only with public readonly properties. If they are private, it's fine.

See #9431 (comment)

@greg0ire
Copy link
Member

Are you using a recent version of Doctrine and Symfony? Since #10187 it's possible to use something else than doctrine/common (symfony/var-dumper). Not saying it will fix your issue, but who knows…

@acelaya
Copy link

acelaya commented Jan 15, 2023

Aha! Worth giving it a try. I'll let you know how it goes

@kiler129
Copy link
Contributor

@greg0ire & @acelaya:

I just tried and... it fails ;) The generated constructor looks like this:

    public function __construct(?\Closure $initializer = null, ?\Closure $cloner = null)
    {
        unset($this->infoPrompt, $this->rulesPrompt);

        $this->__initializer__ = $initializer;
        $this->__cloner__      = $cloner;
    }

Which leads to:

In __CG__AppEntityFacilityLocationCheckInConfiguration.php line 54:
                                                                                                                                                                           
  [Error]                                                                                                                                                                  
  Cannot unset readonly property App\Entity\Facility\LocationCheckInConfiguration::$infoPrompt from scope Proxies\__CG__\App\Entity\Facility\LocationCheckInConfiguration  

The project is pretty new:

% composer show symfony/var-exporter | grep versions
versions : * v6.2.3
% composer show doctrine/orm | grep versions        
versions : * 2.14.0

@acelaya
Copy link

acelaya commented Jan 16, 2023

@greg0ire it works!

@kiler129 it looks like you forgot to set $config->setLazyGhostObjectEnabled(true), because that looks very much like the old kind of proxy. New ones generated with symfony/var-exporter look nothing like that.

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

4 participants