-
-
Notifications
You must be signed in to change notification settings - Fork 504
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
Cannot access uninitialized non-nullable property when accessing a non-db field property of a document #2349
Comments
I'm having the same problem, unfortunately. |
I'm having the same problem with PHP 8-style constructors:
|
@hast constructors are not called by Doctrine initializer as far as I know, unless something changed recently. Reason being this allows you to have your own constructors with enforced data, while Doctrine is "only" interested in setting the relevant fields with values coming from database. If you need to do some set up when Doctrine loads an object, then you can use PostLoad lifecycle event. |
Exactly, like @Steveb-p said - when Doctrine hydrates your entities via Proxy objects, constructor is not called because the remaining data should be hydrated from DB. This is expected behaviour and applies to ORM AFAIK. I understand that sometimes you may need to set some missing stuff in unmanaged properties to make your Document class "valid" but you may achieve that by using #[Document]
#[HasLifecycleCallbacks]
class Customer
{
public function __construct()
{
$this->init();
}
#[PostLoad]
public function init(): void
{
$this->myField = 'test';
}
} |
@Steveb-p I understand that constructor is not called, but in this case those are not constructor logic or initialization, but a new way of declaring properties as of PHP 8. That's why I suppose It must follow the same logic as with old-style property declaration, mustn't it? UPD: Sorry, my initial code snippet was incorrect. What I meant was:
|
I'm afraid this needs to be solved on https://github.com/Ocramius/ProxyManager level |
This should be fixed by #2698. |
Bug Report
Summary
This happens on PHP >= 7.4 where the Document properties have been type hinted.
A fatal error is thrown when you access a property of a Proxy document where the property itself is NOT a field managed by the ODM, even if that property has a default value. This only seems to happen when the Proxy document has been loaded directly from the ODM (i.e. NOT from a __get call from accessing $document->someProperty).
Current behaviour
Throws a fatal error when property is accessed:
How to reproduce
Reproduced on this repo: https://github.com/andythorne/doctrine-odm-example-error
Key bit copied from above repo. See it's documents and command to reproduce the error
Expected behaviour
Any properties should have their default values set from the Document class.
The text was updated successfully, but these errors were encountered: