我试图弄清楚如何在preUpdate
方法中回滚已更改实体的状态(如果有区别,我将使用注释方法)。
我尝试使用EntityManager::refresh($entity)
and 虽然它似乎确实恢复了实体,但它仍然继续并将更改保存到数据库中。
我读过我需要通过调用重新计算更改集,$em->getUnitOfWork()->computeChangeSet($em->getClassMetaData(get_class($this)), $this);
但这并不能解决问题。
反正有没有让UnitOfWork
忽略更改/重新计算正确?
<?php
class Status extends Entity {
/**
* @PreUpdate
*/
public function preUpdate() {
if (!$this->canSave()) {
$this->reset();
}
}
public function reset() {
$em = self::getEntityManager();
$em->refresh($this);
$em->getUnitOfWork()
->computeChangeSet($em->getClassMetaData(get_class($this)), $this);
}
public function canSave() { return false; }
?>
<?php
// Bootstrap for testing/debugging
include 'common.php';
$log = \Zend_Registry::get('log');
$x = Status::find(31); // Retrieve entity from DB
$log->debug("Original: " . $x->getDateLastSeen() . "\n");
$x->setDateLastSeen(new \DateTime());
$log->debug("Set to: " . $x->getDateLastSeen() . "\n");
$x->save();
$log->debug("Saved to: " . $x->getDateLastSeen() . "\n");
输出:
2013-02-14T23:13:22+00:00 DEBUG (7): Original: 2013-02-14 23:08:22
2013-02-14T23:13:22+00:00 DEBUG (7): Set to: 2013-02-14 23:13:22
2013-02-14T23:13:22+00:00 DEBUG (7): Saved to: 2013-02-14 23:08:22
一切看起来都很好,但是当我查看我的数据库日志时,我看到:
409705 Query UPDATE devices_statuses SET date_last_seen = '2013-02-14 23:13:22' WHERE id = 31
409705 Query commit