Union type weirdness: nullable null for non-primitive types #6602

Closed
opened 2026-01-22 15:35:35 +01:00 by admin · 1 comment
Owner

Originally created by @hktr92 on GitHub (Jan 11, 2021).

Hello.

Recently I've updated my PHP version to 8, Symfony to 5.2 and Doctrine 2.8.1.

I'm not sure if the problem is in ORM or DoctrineBundle, but I have an entity defined like so:

class Account {
    private AddressInterface|Address|null $address;
    // ... properties

    public function getAddress(): AddressInterface|Address|null {
        return $this->address;
    }
}

the only reason for using AddressInterface|Address is because of the huge code base and to be a helper to jump around the project.

The problem is that the proxy generates the following invalid PHP code:

    /**
     * {@inheritDoc}
     */
    public function setAddress(\App\Module\Location\Entity\AddressInterface|\App\Model\Entity\Address|?null $address): void
    {
        $this->__initializer__ && $this->__initializer__->__invoke($this, 'setAddress', [$address]);
        parent::setAddress($address);
    }

Using the ?Address notation works perfectly, but I can't say anything about Address|null as the profiler crashes to log (might be another problem in another place for this one, not related to Doctrine).

For now, I'll change back these entities to use the old form.

LE: just for fun, I've changed the return type of getAddress() to be string|int|null, and it generates string|int|?null. so it applies to primitives too.

Originally created by @hktr92 on GitHub (Jan 11, 2021). Hello. Recently I've updated my PHP version to 8, Symfony to 5.2 and Doctrine 2.8.1. I'm not sure if the problem is in ORM or DoctrineBundle, but I have an entity defined like so: ```php class Account { private AddressInterface|Address|null $address; // ... properties public function getAddress(): AddressInterface|Address|null { return $this->address; } } ``` the only reason for using `AddressInterface|Address` is because of the huge code base and to be a helper to jump around the project. The problem is that the proxy generates the following invalid PHP code: ```php /** * {@inheritDoc} */ public function setAddress(\App\Module\Location\Entity\AddressInterface|\App\Model\Entity\Address|?null $address): void { $this->__initializer__ && $this->__initializer__->__invoke($this, 'setAddress', [$address]); parent::setAddress($address); } ``` Using the `?Address` notation works perfectly, but I can't say anything about `Address|null` as the profiler crashes to log (might be another problem in another place for this one, not related to Doctrine). For now, I'll change back these entities to use the old form. LE: just for fun, I've changed the return type of `getAddress()` to be `string|int|null`, and it generates `string|int|?null`. so it applies to primitives too.
admin closed this issue 2026-01-22 15:35:35 +01:00
Author
Owner

@hktr92 commented on GitHub (Jan 14, 2021):

well... after further investigation i reached to the conclusion that this issue is useless to be open here.

it was weird because i thought that doctrine/orm relies on ocramius/proxy-manager, but apparently, doctrine/migrations relies on friendsofphp/proxy-manager-lts which replaces ocramius/proxy-manager and they can't do anything unless ocramius/proxy-manager doesn't have full php 8 support.

oh boi, I love open source! [/sarcasm]

@hktr92 commented on GitHub (Jan 14, 2021): well... after further investigation i reached to the conclusion that this issue is useless to be open here. it was weird because i thought that `doctrine/orm` relies on `ocramius/proxy-manager`, but apparently, `doctrine/migrations` relies on `friendsofphp/proxy-manager-lts` which replaces `ocramius/proxy-manager` and they can't do anything unless `ocramius/proxy-manager` doesn't have full php 8 support. oh boi, I _love_ open source! [/sarcasm]
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6602