Skip to content

ManyToMany

Tom Anderson edited this page Mar 18, 2016 · 1 revision

This isn't required in the target project at the time of this writing. Where the project was left at SoliantEntityAudit was the author wrestling with how to find these relations and how to audit them. You see, if you have an entity to audit and it has a large many to many relation the Doctrine way to do it is to fetch every entity the table is joined with.

Given entity1 and entity2 there exists a table between them but not an entity. Getting the join information is like

foreach ($entity->getJoinedEntity() as $joinedEntity) {
    $auditManyToManyEntity = new RevisionEntity\AuditManyToManyEntity();
    $auditManyToManyEntity->setJoinedEntityId($joinedEntity->getId());
    $auditManyToManyEntity->setEntityId($entity->getId());
    $auditManyToManyEntity->setAuditEntity($auditEntity);
    
    $auditObjectManager->persist($auditManyToManyEntity);
}

I hope this example shows the expense required to audit the many to many relationship. Delta auditing of the many to many relationships is possible but seems like an even less good idea.

This problem of auditing many to many also exists in the SimpleThings EntityAudit

Clone this wiki locally