尽管这个问题有点老了,但我只是找到了一种更优雅的方法来解决我想使用 Doctrine ORM 2.6.2 在这里分享的这个问题:
您可以简单地告诉ClassMetadataInfo
您的表的对象只是将这些字段声明为您传递propertyChanged
给UnitOfWork
. 这里的重要部分是setChangeTrackingPolicy和propertyChanged调用:
$unitOfWork = $entityManager->getUnitOfWork();
$classMeta = $entityManager->getClassMetadata('Your\Table\Class');
$ctp = $classMeta->changeTrackingPolicy; # not a function but a variable
# Tell the table class to not automatically calculate changed values but just
# mark those fields as dirty that get passed to propertyChanged function
$classMeta->setChangeTrackingPolicy(ORM\ClassMetadataInfo::CHANGETRACKING_NOTIFY);
# tell the unit of work that an value has changed no matter if the value
# is actually different from the value already persistent
$oldValue = $entity->getValue('fieldName'); # some custom implementation or something
$newValue = $oldValue;
$unitOfWork->propertyChanged($entity, 'fieldName', $oldValue, $newValue);
# field value will be updated regardless of whether its PHP value has actually changed
$entityManager->flush();
# set the change tracking policy back to the previous value to not mess things up
$classMeta->setChangeTrackingPolicy($ctp);
您可能还想看看Doctrine\Common\NotifyPropertyChanged
界面。
我希望这对某人有用。