我想跟踪不同实体的变化,并从其他表中引用特定版本。例如:在Orderline
表中,我想引用产品的特定版本。
Loggable
扩展是实现此功能的最佳方式,还是应该手动添加产品版本实体?
我目前正在使用Loggable
,我认为我缺少像$product->getCurrentVersion()
这样的功能来获取当前版本号。还是我误读了文档?
您可以在
存储库中实现此功能以获取当前/最新版本
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
}
使用可定位扩展,这可以通过以下方式完成:
$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();
}
我也可以推荐你 实体审计扩展:https://github.com/simplethings/EntityAudit
在您的情况下,这将是:
$auditReader = $this->container->get("simplethings_entityaudit.reader");
$revision = $auditReader->getCurrentRevision(
'YourBundle'Entity'Product',
$id
);
// current version number
$revision->getRev();
您还可以:
- 查找特定修订的实体状态
- 查找特定修订版的更改图元
- 查找被审计实体的修订历史记录
我建议看看Doctrine 2的扩展 -> EntityAudit(有一个关于如何在Symfony2中安装它的解释)。
正如您在文档中读到的,
教义 2 的扩展受到 Hibernate Envers 和 允许对实体及其关联进行完整版本控制。
用法非常简单。安装后,您可以执行以下操作:
- 指定要审核的实体。让我们从产品开始:
/config.yml
simple_things_entity_audit:
audited_entities:
- MyBundle'Entity'Product
- 在数据库上启动更新以创建必要的表:
./app/console doctrine:schema:update --force
然后从您的控制器:
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! } .... } }
希望对您有所帮助。
亲切的问候和新年快乐。