Use loggable to link to product version on order line?
I would like to track changes to different objects and reference a specific version from other tables. For example: in the table Orderline
I would like to refer to a specific version of the product.
Is an Loggable
extension the best way to implement this functionality or should I manually add the ProductVersion object?
I am using Loggable
at the moment and I think I am missing a type function $product->getCurrentVersion()
to get the current version number. Or am I reading the documentation incorrectly?
source to share
You can implement this feature in your repository to get the current / latest 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
}
source to share
Using the 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 the EntityAudit extension to you : https://github.com/simplethings/EntityAudit
In your case, it would be:
$auditReader = $this->container->get("simplethings_entityaudit.reader");
$revision = $auditReader->getCurrentRevision(
'YourBundle\Entity\Product',
$id
);
// current version number
$revision->getRev();
You also can:
- find the state of an object at a specific revision
- find changed objects at a specific revision
- find the history of changes of the checked object
source to share
I would suggest looking at this extension for Doctrine 2 - > EntityAudit (it explains how to install it in Symfony2).
As you can read in the documentation,
This Doctrine 2 extension is inspired by the Hibernate Envers and allows full versioning of objects and their associations.
The usage is pretty simple. You can do the following after installation:
- Indicate the objects that you want to check. Let's start with the product:
app /Config/config.yml
simple_things_entity_audit:
audited_entities:
- MyBundle\Entity\Product
- Run your database upgrade to create the required tables:
./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 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.
Best wishes and Happy New Year.
source to share