I have a custom entity that is not revisionable and I need to store any differences made by the user - and only differences, so if nothing has changed there should be no entry.

I am doing this in preSave method because the original entity has the same format as current entity(ie. some fields do some value processing and therefore the original entity does not match the current entity exactly in postSave method).

The issue I am facing is that I cannot properly compare values($entity->toArray()) because the data is not consistent. For example in the original entity a field's value can be 123.8 but in the current entity it can be 123.8999999... If I type cast it to string I end up with something like '123.8' vs '123.80' so the values won't match. Or 1 can be '1' and so on. Also with arrays I think keys get misordered sometimes(either that or the same issue but in the whole array).

I have tried to use the DiffArray::diffAssocRecursive() and Differ::diff() but neither is working reliably.

One thing that comes to mind is doing something like Differ::diff($entity->view('somemodewithallfield'), $entity->original->view('somemodewithallfield')) but that won't cover fields that are hidden or invisible in UI and also diff would be "ugly" and too big.

So I am wonder what would be the correct solution in this case? I would like to avoid going through each value and doing some insane comparisons and data transformations(string/int/float).

I remember that there was a story on d.org to not save any field data if the data hasn't changed but I haven't looked into that yet and I'm not sure at what state that is at the moment.

1 Answer 1


Assuming you have two entities $a and $b.

You can convert them to arrays and compare the two arrays.

// if the position matters
$is_equal = ($a->toArray() === $b->toArray())

// if the position doesn't matter
$is_equal = ($a->toArray() == $b->toArray())

Edit: I thought the OP was about discovering if two entities have identical data. After reading the question again it seems that OP wants a way to mark what is the difference in data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.