I would like to track changes of different entities and refer to a specific version from other tables. For example: in the Orderline
table, I would like to refer to a specific version of a product.
Is the Loggable
extension the best way to implement this feature or should I manualy add a ProductVersion entity?
I'm using Loggable
at this moment and I think I'm missing a feature like $product->getCurrentVersion()
to get the current version number. Or do I misread the documentation?
You can implement this function in your Repository to get current/last version
public function getCurrentVersion($id)
{
$repo = $this->_em->getRepository('Gedmo\Loggable\Entity\LogEntry');
$log = $repo->findOneBy(array('objectId' =>$id), array('version' => 'desc'));
return $log ? $log->getVersion() : null; // or return $log for entire object
}
Using Loggable extension this can be done with:
$repo = $em->getRepository('Gedmo\Loggable\Entity\LogEntry');
// your Product entity
$product = $em->find('Entity\Product', $id);
// find revisions for given product
$logs = $repo->getLogEntries($product);
if (count($logs) > 0) {
// as it is sorted descending by version
$currentVersion = $repo->getLogEntries($product)[0]->getVersion();
}
I can also recommend you EntityAudit extension: https://github.com/simplethings/EntityAudit
In your case that would be:
$auditReader = $this->container->get("simplethings_entityaudit.reader");
$revision = $auditReader->getCurrentRevision(
'YourBundle\Entity\Product',
$id
);
// current version number
$revision->getRev();
You can also:
I'd suggest having a look at this extension for Doctrine 2 -> EntityAudit (there's an explanation on how to install it in Symfony2).
As you can read in the documentation,
This extension for Doctrine 2 is inspired by Hibernate Envers and allows full versioning of entities and their associations.
The usage is pretty simple. You could do the following once you have it installed:
app/config/config.yml
simple_things_entity_audit:
audited_entities:
- MyBundle\Entity\Product
./app/console doctrine:schema:update --force
And then from your controller:
class DefaultController extends Controller {
public function indexAction() {
....
$auditReader = $this->container->get("simplethings_entityaudit.reader");
foreach( $orderLine as $product ) {// Let's assume for simplicity that this makes sense.
$productAudit = $auditReader->find(
'SimpleThings\EntityAudit\Tests\ProductAudit',
$id = $product->getId(),
$rev = 10 // Number of the revision you're interested in.
);
// Do whatever you please with the estate of the entity at revision 10!
}
....
}
}
Hope it helps.
Kind regards and happy new year.