I use Loggable behavioral extension to log changes in my entities. I want to log changes in manyToMany relations too. I want to show to user this kind of change log:
Since i cannot add a comment to the accepted answer, i ll write here :)
Accepted solution will not work if you have multiple persists of your main entity in same flush. The last set of ManyToMany collection will be attached to all persisted entities. If you want to pick only the approriate you will have to check if the collection belongs to the processed object.
For example instead of
// For each collection add it to the return array in our custom format.
foreach ($uow->getScheduledCollectionUpdates() as $col) {
$associations = $this->getCollectionChangeSetData($col);
$returnArray = array_merge($returnArray, $associations);
}
you can use
// For each collection add it to the return array in our custom format.
$objectHash = spl_object_hash($object);
foreach ($uow->getScheduledCollectionUpdates() as $col) {
$collectionOwner = $col->getOwner();
if (spl_object_hash($collectionOwner) === $objectHash) {
$associations = $this->getCollectionChangeSetData($col);
$returnArray = array_merge($returnArray, $associations);
}
}