那么...当我们想要在 Doctrine 生命周期之外找到变更集时该怎么办?正如我在上面对@Ocramius 帖子的评论中提到的那样,也许可以创建一个“只读”方法,它不会与实际的 Doctrine 持久性混淆,但可以让用户了解发生了什么变化。
这是我所想的一个例子......
/**
* Try to get an Entity changeSet without changing the UnitOfWork
*
* @param EntityManager $em
* @param $entity
* @return null|array
*/
public static function diffDoctrineObject(EntityManager $em, $entity) {
$uow = $em->getUnitOfWork();
/*****************************************/
/* Equivalent of $uow->computeChangeSet($this->em->getClassMetadata(get_class($entity)), $entity);
/*****************************************/
$class = $em->getClassMetadata(get_class($entity));
$oid = spl_object_hash($entity);
$entityChangeSets = array();
if ($uow->isReadOnly($entity)) {
return null;
}
if ( ! $class->isInheritanceTypeNone()) {
$class = $em->getClassMetadata(get_class($entity));
}
// These parts are not needed for the changeSet?
// $invoke = $uow->listenersInvoker->getSubscribedSystems($class, Events::preFlush) & ~ListenersInvoker::INVOKE_MANAGER;
//
// if ($invoke !== ListenersInvoker::INVOKE_NONE) {
// $uow->listenersInvoker->invoke($class, Events::preFlush, $entity, new PreFlushEventArgs($em), $invoke);
// }
$actualData = array();
foreach ($class->reflFields as $name => $refProp) {
$value = $refProp->getValue($entity);
if ($class->isCollectionValuedAssociation($name) && $value !== null) {
if ($value instanceof PersistentCollection) {
if ($value->getOwner() === $entity) {
continue;
}
$value = new ArrayCollection($value->getValues());
}
// If $value is not a Collection then use an ArrayCollection.
if ( ! $value instanceof Collection) {
$value = new ArrayCollection($value);
}
$assoc = $class->associationMappings[$name];
// Inject PersistentCollection
$value = new PersistentCollection(
$em, $em->getClassMetadata($assoc['targetEntity']), $value
);
$value->setOwner($entity, $assoc);
$value->setDirty( ! $value->isEmpty());
$class->reflFields[$name]->setValue($entity, $value);
$actualData[$name] = $value;
continue;
}
if (( ! $class->isIdentifier($name) || ! $class->isIdGeneratorIdentity()) && ($name !== $class->versionField)) {
$actualData[$name] = $value;
}
}
$originalEntityData = $uow->getOriginalEntityData($entity);
if (empty($originalEntityData)) {
// Entity is either NEW or MANAGED but not yet fully persisted (only has an id).
// These result in an INSERT.
$originalEntityData = $actualData;
$changeSet = array();
foreach ($actualData as $propName => $actualValue) {
if ( ! isset($class->associationMappings[$propName])) {
$changeSet[$propName] = array(null, $actualValue);
continue;
}
$assoc = $class->associationMappings[$propName];
if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE) {
$changeSet[$propName] = array(null, $actualValue);
}
}
$entityChangeSets[$oid] = $changeSet; // @todo - remove this?
} else {
// Entity is "fully" MANAGED: it was already fully persisted before
// and we have a copy of the original data
$originalData = $originalEntityData;
$isChangeTrackingNotify = $class->isChangeTrackingNotify();
$changeSet = $isChangeTrackingNotify ? $uow->getEntityChangeSet($entity) : array();
foreach ($actualData as $propName => $actualValue) {
// skip field, its a partially omitted one!
if ( ! (isset($originalData[$propName]) || array_key_exists($propName, $originalData))) {
continue;
}
$orgValue = $originalData[$propName];
// skip if value haven't changed
if ($orgValue === $actualValue) {
continue;
}
// if regular field
if ( ! isset($class->associationMappings[$propName])) {
if ($isChangeTrackingNotify) {
continue;
}
$changeSet[$propName] = array($orgValue, $actualValue);
continue;
}
$assoc = $class->associationMappings[$propName];
// Persistent collection was exchanged with the "originally"
// created one. This can only mean it was cloned and replaced
// on another entity.
if ($actualValue instanceof PersistentCollection) {
$owner = $actualValue->getOwner();
if ($owner === null) { // cloned
$actualValue->setOwner($entity, $assoc);
} else if ($owner !== $entity) { // no clone, we have to fix
// @todo - what does this do... can it be removed?
if (!$actualValue->isInitialized()) {
$actualValue->initialize(); // we have to do this otherwise the cols share state
}
$newValue = clone $actualValue;
$newValue->setOwner($entity, $assoc);
$class->reflFields[$propName]->setValue($entity, $newValue);
}
}
if ($orgValue instanceof PersistentCollection) {
// A PersistentCollection was de-referenced, so delete it.
// These parts are not needed for the changeSet?
// $coid = spl_object_hash($orgValue);
//
// if (isset($uow->collectionDeletions[$coid])) {
// continue;
// }
//
// $uow->collectionDeletions[$coid] = $orgValue;
$changeSet[$propName] = $orgValue; // Signal changeset, to-many assocs will be ignored.
continue;
}
if ($assoc['type'] & ClassMetadata::TO_ONE) {
if ($assoc['isOwningSide']) {
$changeSet[$propName] = array($orgValue, $actualValue);
}
// These parts are not needed for the changeSet?
// if ($orgValue !== null && $assoc['orphanRemoval']) {
// $uow->scheduleOrphanRemoval($orgValue);
// }
}
}
if ($changeSet) {
$entityChangeSets[$oid] = $changeSet;
// These parts are not needed for the changeSet?
// $originalEntityData = $actualData;
// $uow->entityUpdates[$oid] = $entity;
}
}
// These parts are not needed for the changeSet?
//// Look for changes in associations of the entity
//foreach ($class->associationMappings as $field => $assoc) {
// if (($val = $class->reflFields[$field]->getValue($entity)) !== null) {
// $uow->computeAssociationChanges($assoc, $val);
// if (!isset($entityChangeSets[$oid]) &&
// $assoc['isOwningSide'] &&
// $assoc['type'] == ClassMetadata::MANY_TO_MANY &&
// $val instanceof PersistentCollection &&
// $val->isDirty()) {
// $entityChangeSets[$oid] = array();
// $originalEntityData = $actualData;
// $uow->entityUpdates[$oid] = $entity;
// }
// }
//}
/*********************/
return $entityChangeSets[$oid];
}
这里将其表述为静态方法,但可以成为 UnitOfWork 内部的方法...?
我没有跟上 Doctrine 的所有内部细节,所以可能错过了一些有副作用的东西或误解了该方法的部分功能,但对它的(非常)快速测试似乎给了我我希望看到的结果。
我希望这对某人有帮助!