Reputation: 1016
I'm developing web application. The most important feature should be history(versioning) and approving inserted data by user. It means that user can see all history changes, use old version again, validate or refuse a new version, which is inserted by user without permision.
I have one table actionHistory(date,user,action,isValid) then I have e.g. Person, Address, CustomField and CustomValue tables. Person manyToMany Address. Person oneToMany CustomFields. CustomFields oneToOne CustomValue. Every table also have a historical table.
E.G. When I am persisting person with custom field-value. I have to create person,customfield-value history tables. Copy this versions into them. Connect all historical tables and also connect with actionHistory
.
At the beginning I've used EventListener and Lifecycle Event (postPersist,postDelete,postUpdate), but the main problem is that when I would like to add previous example I have always problem to connect these tables. I don't know which entity will persist first and when I try the second entity doesn't exist yet,etc.
public function postUpdate(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
$em = $args->getEntityManager();
if ($entity instanceof Address) {
$this->addPersonHistory($em, $entity);
$this->saveAddressHistory("UPDATE", $em, $entity, "Address");
}
if ($entity instanceof CustomField) {
$this->addCustomFieldHistory($em, $entity);
$this->saveCustomFieldHistory("UPDATE", $em, $entity, "CustomField");
}
if ($entity instanceof CustomValue) {
$this->saveCustomValueHistory("UPDATE", $em, $entity, "CustomValue");
}
if ($entity instanceof Person) {
$this->savePersonHistory("UPDATE", $em, $entity, "Person");
}
}
Is there some better solution for this situation? Because I am stuck now, when I have to face form adding of person-customField-customValue.
Thank you in advance!
Upvotes: 1
Views: 2039
Reputation: 12727
What you are looking for exists (good news).
It's done with the DoctrineExtensions
of Gedmo, provided by repository : https://github.com/Atlantic18/DoctrineExtensions
The extension you need is Loggable, and explained here : https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/loggable.md
And the best is that there is a bundle that directly implements those extensions : https://github.com/stof/StofDoctrineExtensionsBundle Explanations here : https://github.com/stof/StofDoctrineExtensionsBundle/blob/master/Resources/doc/index.rst
Upvotes: 4