Use loggable to refer to a version of Product in Order Line?

1.7k views Asked by At

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?

3

There are 3 answers

0
Abdallah Arffak On BEST ANSWER

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
}
0
acontell On

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:

  1. Specify the entities that you want to audit. Let's start by Product:

app/config/config.yml

simple_things_entity_audit:
    audited_entities:
        - MyBundle\Entity\Product
  1. Launch an update on your DB to create the necessary tables:

./app/console doctrine:schema:update --force

  1. 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.

0
Kamil Adryjanek On

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:

  • find entity state at a particular revision
  • find changed entities at a specific revision
  • find revision history of an audited entity