nextras / orm

Orm with clean object design, smart relationship loading and powerful collections.
https://nextras.org/orm
MIT License
309 stars 59 forks source link

Null value exception when removing an entity with 1:1 relationship #654

Closed jiripudil closed 8 months ago

jiripudil commented 8 months ago

Hi, copying a bug report from Slack so that it can be tracked.

Relevant pieces of entities:

/**
 * @property Mutation $mutation {m:1 Mutation, oneSided=true}
 * @property OrderDelivery|null $delivery {1:1 OrderDelivery::$order, isMain=true, cascade=[persist, remove]}
 */
class Order
{
}

/**
 * @property Order $order {1:1 Order::$delivery}
 */
class OrderDelivery
{
}

When I try to $orm->remove($order) where $order->delivery !== null, orm throws NullValueException because it tries to set null to $order->mutation. A debugging session led me into RemovalHelper::getCascadeQueueAndSetNulls and I am quite certain the culprit is there.

The getRelationships($order) method call here returns:

Since #235, Orm seems to handle this situation for the owning side but not for the reverse side (which is our case). I'm not sure why or what the consequences of introducing it would be.

(Yep, it would likely help to switch the sides of the relationship, but I feel that it should not matter. I want to remove the whole thing anyway.)

orm 4.0.7, dbal 4.0.5

hrach commented 8 months ago

Thank you for this report! Aren't there any other relationships? This seems quite similar to removing a book in our test suite (it has also a 1:1 to Ean and m:1 to other things) and it works. So I'd start with a reproduction.

jiripudil commented 8 months ago

Sure, I've pushed a minimal code to reproduce this to https://github.com/jiripudil/nextras-orm-bug-654

hrach commented 8 months ago

@jiripudil I've cherrypicked the fix to v4 branch, please test if it works for you. Thank you.

jiripudil commented 8 months ago

@hrach Works like a charm, thank you!