使用loggable来引用订单行中的产品版本?

时间:2014-12-22 10:53:05

标签: php symfony doctrine-orm doctrine-extensions stofdoctrineextensions

我想跟踪不同实体的更改,并参考其他表中的特定版本。例如:在Orderline表中,我想引用产品的特定版本。

Loggable extension是实现此功能的最佳方式,还是我应该手动添加ProductVersion实体?

此时我正在使用Loggable我认为我缺少像$product->getCurrentVersion()这样的功能来获取当前版本号。或者我误读了文档?

3 个答案:

答案 0 :(得分:2)

您可以在存储库中实现此功能以获取当前/最新版本

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
}

答案 1 :(得分:2)

使用可记录扩展程序可以通过以下方式完成:

$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();
}

我还可以向您推荐 EntityAudit 扩展程序:https://github.com/simplethings/EntityAudit

在你的情况下:

$auditReader = $this->container->get("simplethings_entityaudit.reader");
$revision = $auditReader->getCurrentRevision(
    'YourBundle\Entity\Product',
    $id
);
// current version number
$revision->getRev();

你也可以:

  • 在特定版本中查找实体状态
  • 查找特定版本的已更改实体
  • 查找被审计实体的修订历史记录

答案 2 :(得分:1)

我建议看一下Doctrine 2的这个扩展 - > EntityAudit(有关于如何在Symfony2中安装它的解释)。

正如您可以在文档中看到的那样,

  

Doctrine 2的这个扩展程序的灵感来自于Hibernate Envers和   允许实体及其关联的完整版本。

用法非常简单。安装完成后,您可以执行以下操作:

  1. 指定要审核的实体。让我们从产品开始:
  2. 应用/配置/ config.yml

    simple_things_entity_audit:
        audited_entities:
            - MyBundle\Entity\Product
    
    1. 在您的数据库上启动更新以创建必要的表:
    2. ./app/console doctrine:schema:update --force

      1. 然后从您的控制器:

        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!
              }
              ....
            }
        }
        
      2. 希望它有所帮助。

        亲切的问候和新年快乐。