As designed Deleting relation doesn't remove relation from parent

Liam W

in memoriam 1998-2020
Affected version
2.0.0 DP10
Sorry for the wordy title, but I think it makes sense...

Essentially, if I delete a relation on an Entity, and then try and access that relation again, it works fine without problems. Surely, as it's been deleted, the relation should be removed from the parent entity?

Liam
 
A relation should, in theory, return either null or an empty set of data if it doesn't exist anymore. Is that not the behaviour you're seeing? I haven't tested it out.
 
Yeah, the relation entity still has the values array set, so even the exists method returns true.

Liam
 
So this is mostly generally expected and unavoidable. If you load a relation, it's then cached within the entity unless action is taken in the entity itself to remove it. I think this is probably desirable in many cases, though more specifically it's basically unavoidable (short of not caching, which isn't viable). Consider code like (not that you'd tend to do this):
Code:
$ent->Relation->delete();
$ent->Relation->get('value');

// ####### versus ##########

$relation = $ent->Relation;
$relation->delete();
$relation->get('value');
With what you're proposing, those would actually behave differently. (Continuing to work with an existing entity object after deletion is totally valid.)

Regardless, there's no technical viability to any sort of change to this behavior. (It would require a reference counting system that actually knew what held the references to unset them.) So in that regard, this is generally as designed.

However, there are some changes that have been made:
  1. There's a new isDeleted() method on entities that allow you to detect if an entity has been removed already.
  2. Any entity that has been deleted will always return false from exists(), isUpdate() and isInsert().
  3. Attempting to save a deleted entity will throw an exception (you need a new entity object to save).
 
Top Bottom