As designed Deleting relation doesn't remove relation from parent

Affected version
2.0.0 DP10

Liam W

Well-known member
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?



Well-known member
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.

Liam W

Well-known member
Yeah, the relation entity still has the values array set, so even the exists method returns true.



XenForo developer
Staff member
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):

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

$relation = $ent->Relation;
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).