Easyadminbundle: Add support to typed properties of PHP 7.4

Created on 19 Feb 2020  路  6Comments  路  Source: EasyCorp/EasyAdminBundle

Describe the bug
If you try to create a new registry of a entity that are using typed properties, it will fail.

To Reproduce
Using PHP 7.4.2 and EasyAdmin v2.3.5, change one of your entities and add a typed property. In this case note the _int_ before _$id_
/** * @ORM\Id() * @ORM\GeneratedValue() * @ORM\Column(type="integer", options={"unsigned"=true}) */ private int $id;
Then try to add a new registry and you can see this error

Typed property App\Entity\YourEntity::$id must not be accessed before initialization

bug unconfirmed

Most helpful comment

I guess the issue is that the Form component needs to read the data from your entity while it isn't initialised yet. A similar issue occurs when you use type declarations for your getter/setter methods without allowing null to be set/to be returned. The solution is to use nullable type delcarations for your properties and initialise them with null.

Yes it works, but it's a bit weird because the id can not be null

  /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer", options={"unsigned"=true})
     */
    private ?int $id = null;

All 6 comments

Thanks for reporting this. To be honest, I don't know where is that error coming from? Is the error message really generated by EasyAdmin? Could it be generated instead by Doctrine or Symfony? Thanks.

I started to use typed properties just after switch to PHP 7.4 and Doctrine didn't work https://github.com/doctrine/orm/issues/7854 and later was fixed https://github.com/doctrine/orm/pull/7857.
And my website works fine with them except in the great EasyAdmin :)

I guess the issue is that the Form component needs to read the data from your entity while it isn't initialised yet. A similar issue occurs when you use type declarations for your getter/setter methods without allowing null to be set/to be returned. The solution is to use nullable type delcarations for your properties and initialise them with null.

I guess the issue is that the Form component needs to read the data from your entity while it isn't initialised yet. A similar issue occurs when you use type declarations for your getter/setter methods without allowing null to be set/to be returned. The solution is to use nullable type delcarations for your properties and initialise them with null.

Yes it works, but it's a bit weird because the id can not be null

  /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer", options={"unsigned"=true})
     */
    private ?int $id = null;

Well, when you bind it to a form and allow it to be uninitialised when the form is populated, you will either need to ensure it has a valid value or make sure it isn't accessed before being initialised.

Let's close as "fixed" ... although the solution is not perfect. Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nickicool picture nickicool  路  4Comments

haithem-rihane picture haithem-rihane  路  4Comments

bocharsky-bw picture bocharsky-bw  路  3Comments

joazvsoares picture joazvsoares  路  4Comments

ghost picture ghost  路  3Comments