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:
+--------------------------------------------------+
| Article "My Article" change log: |
+-------+------------+-----------------------------+
| Who | When | What |
+-------+------------+-----------------------------+
| Admin | 2015-07-01 | Removed tags "tag1", "tag2" |
| Admin | 2015-07-01 | Added tags "tag3" |
+-------+------------+-----------------------------+
Event problem
I think, Doctrine doesn't fire events when manyToMany relation changes, so Loggable (listening doctrine events) doesn't save log entry. I can work around it by creating my own manyToMany table, but here's go the second problem:
Own ManyToMany problem
When I create entity representing manyToMany relation without @JoinTable annotation, I don't know, how to write the new entity to behave like the old JoinTable one. I want no BC break. Can you give me a clue, how Doctrine handles this?
Do you have any recommendation, how to log changes in manyToMany relations?
Solution without creating your own join tables.
I have modified the LoggableListener that I created to override the Gedmo LoggableListener, my version works, play around with this till you get it working.
Basically, extend the Gedmo LoggableListener with your own version and override /add a few modified functions:
prePersistLogEntry is enabled to allow you to modify the logEntry if you want to. My logEntry entities contain a user entity and the users Full Name instead of their username.
getCollectionsChangeSetData is a new function to extract the collection and get access to the Doctrine PersistentCollections methods. [http://www.doctrine-project.org/api/orm/2.1/class-Doctrine.ORM.PersistentCollection.html]
stripCollectionArray new function to extract the desired information from the collection entities and insert them into a php array for persisting to the LogEntry.
For information, if you are planning to user the revert functionality of the Loggable doctrine extension then you will also need to extend and override the revert method in the LogEntryRepository. The current revert method will not recognise the id from the ManyToMany associations saved in the LogEntry. That is why stripCollectionArray function also saves the 'id' and 'class' values to the LogEntry.
Good Luck.