3

我想跟踪不同实体的变化并从其他表中引用特定版本。例如:在Orderline表格中,我想引用一个产品的特定版本。

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

我现在正在使用Loggable,我想我缺少一个功能,比如$product->getCurrentVersion()获取当前版本号。还是我误读了文档?

4

3 回答 3

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
}
于 2014-12-29T15:45:34.583 回答
2

使用Loggable扩展可以通过以下方式完成:

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

你也可以:

  • 查找特定版本的实体状态
  • 在特定修订中查找更改的实体
  • 查找被审计实体的修订历史
于 2014-12-31T10:18:35.760 回答
1

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

正如您在文档中所读到的,

Doctrine 2 的这个扩展受到 Hibernate Envers 的启发,并允许对实体及其关联进行完整版本控制。

用法很简单。安装后,您可以执行以下操作:

  1. 指定要审核的实体。让我们从产品开始:

应用程序/配置/config.yml

simple_things_entity_audit:
    audited_entities:
        - MyBundle\Entity\Product
  1. 在您的数据库上启动更新以创建必要的表:

./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!
          }
          ....
        }
    }
    

希望能帮助到你。

亲切的问候和新年快乐。

于 2014-12-31T21:18:42.843 回答