/** * Get a documents actual data, flattening all the objects to arrays. * * @param object $document * @return array */ public function getDocumentActualData($document) { $class = $this->dm->getClassMetadata(get_class($document)); $actualData = array(); foreach ($class->reflFields as $name => $refProp) { $mapping = $class->fieldMappings[$name]; $value = $refProp->getValue($document); if (isset($mapping['file']) && !$value instanceof GridFSFile) { $value = new GridFSFile($value); $class->reflFields[$name]->setValue($document, $value); $actualData[$name] = $value; } elseif (($class->isCollectionValuedReference($name) || $class->isCollectionValuedEmbed($name)) && $value !== null && !$value instanceof PersistentCollection) { // If $actualData[$name] is not a Collection then use an ArrayCollection. if (!$value instanceof Collection) { $value = new ArrayCollection($value); } // Inject PersistentCollection $coll = new PersistentCollection($value, $this->dm, $this, $this->cmd); $coll->setOwner($document, $mapping); $coll->setDirty(!$value->isEmpty()); $class->reflFields[$name]->setValue($document, $coll); $actualData[$name] = $coll; } else { if (!$class->isIdentifier($name) || $class->isIdGeneratorNone()) { $actualData[$name] = $value; } } } return $actualData; }
/** * Get a documents actual data, flattening all the objects to arrays. * * @param object $document * @return array */ public function getDocumentActualData($document) { $class = $this->dm->getClassMetadata(get_class($document)); $actualData = array(); foreach ($class->reflFields as $name => $refProp) { $mapping = $class->fieldMappings[$name]; // skip not saved fields if (isset($mapping['notSaved']) && $mapping['notSaved'] === true) { continue; } $value = $refProp->getValue($document); if (isset($mapping['file']) && !$value instanceof GridFSFile) { $value = new GridFSFile($value); $class->reflFields[$name]->setValue($document, $value); $actualData[$name] = $value; } elseif (isset($mapping['association']) && $mapping['type'] === 'many' && $value !== null && !$value instanceof PersistentCollection) { // If $actualData[$name] is not a Collection then use an ArrayCollection. if (!$value instanceof Collection) { $value = new ArrayCollection($value); } // Inject PersistentCollection $coll = new PersistentCollection($value, $this->dm, $this); $coll->setOwner($document, $mapping); $coll->setDirty(!$value->isEmpty()); $class->reflFields[$name]->setValue($document, $coll); $actualData[$name] = $coll; } else { $actualData[$name] = $value; } } return $actualData; }
/** * Executes a merge operation on an document. * * @param object $document * @param array $visited * @return object The managed copy of the document. * @throws InvalidArgumentException If the document instance is NEW. */ private function doMerge($document, array &$visited, $prevManagedCopy = null, $assoc = null) { $oid = spl_object_hash($document); if (isset($visited[$oid])) { return; // Prevent infinite recursion } $visited[$oid] = $document; // mark visited $class = $this->dm->getClassMetadata(get_class($document)); // First we assume DETACHED, although it can still be NEW but we can avoid // an extra db-roundtrip this way. If it is not MANAGED but has an identity, // we need to fetch it from the db anyway in order to merge. // MANAGED documents are ignored by the merge operation. if ($this->getDocumentState($document, self::STATE_DETACHED) == self::STATE_MANAGED) { $managedCopy = $document; } else { $id = null; if (!$class->isEmbeddedDocument) { // Try to look the entity up in the identity map. $id = $class->getIdentifierValue($document); } // If there is no ID, it is actually NEW. if (!$id) { $managedCopy = $class->newInstance(); $this->persistNew($class, $managedCopy); } else { $managedCopy = $this->tryGetById($id, $class->rootDocumentName); if ($managedCopy) { // We have the entity in-memory already, just make sure its not removed. if ($this->getDocumentState($managedCopy) == self::STATE_REMOVED) { throw new InvalidArgumentException('Removed entity detected during merge.' . ' Can not merge with a removed entity.'); } } else { // We need to fetch the managed copy in order to merge. $managedCopy = $this->dm->find($class->name, $id); } if ($managedCopy === null) { // If the identifier is ASSIGNED, it is NEW, otherwise an error // since the managed entity was not found. $managedCopy = $class->newInstance(); $class->setIdentifierValue($managedCopy, $id); $this->persistNew($class, $managedCopy); } } if ($class->isVersioned) { $managedCopyVersion = $class->reflFields[$class->versionField]->getValue($managedCopy); $documentVersion = $class->reflFields[$class->versionField]->getValue($document); // Throw exception if versions don't match. if ($managedCopyVersion != $documentVersion) { throw LockException::lockFailedVersionMissmatch($documentVersion, $managedCopyVersion); } } // Merge state of $document into existing (managed) entity foreach ($class->reflFields as $name => $prop) { if (!isset($class->fieldMappings[$name]['embedded']) && !isset($class->fieldMappings[$name]['reference'])) { $prop->setValue($managedCopy, $prop->getValue($document)); } else { $assoc2 = $class->fieldMappings[$name]; if ($assoc2['type'] === 'one') { $other = $prop->getValue($document); if ($other === null) { $prop->setValue($managedCopy, null); } else { if ($other instanceof Proxy && !$other->__isInitialized__) { // do not merge fields marked lazy that have not been fetched. continue; } else { if (!isset($assoc2['embedded']) && !$assoc2['isCascadeMerge']) { if ($this->getDocumentState($other, self::STATE_DETACHED) == self::STATE_MANAGED) { $prop->setValue($managedCopy, $other); } else { $targetDocument = isset($assoc2['targetDocument']) ? $assoc2['targetDocument'] : get_class($other); $targetClass = $this->dm->getClassMetadata($targetDocument); $id = $targetClass->getIdentifierValue($other); $proxy = $this->dm->getProxyFactory()->getProxy($targetDocument, $id); $prop->setValue($managedCopy, $proxy); $this->registerManaged($proxy, $id, array()); } } } } } else { $mergeCol = $prop->getValue($document); if ($mergeCol instanceof PersistentCollection && !$mergeCol->isInitialized()) { // do not merge fields marked lazy that have not been fetched. // keep the lazy persistent collection of the managed copy. continue; } foreach ($mergeCol as $entry) { $targetDocument = isset($assoc2['targetDocument']) ? $assoc2['targetDocument'] : get_class($entry); $targetClass = $this->dm->getClassMetadata($targetDocument); if ($targetClass->isEmbeddedDocument) { $this->registerManaged($entry, null, array()); } else { $id = $targetClass->getIdentifierValue($entry); $this->registerManaged($entry, $id, array()); } } if (!$mergeCol instanceof PersistentCollection) { if (!$mergeCol instanceof Collection) { $mergeCol = new ArrayCollection($mergeCol); } $mergeCol = new PersistentCollection($mergeCol, $this->dm, $this, $this->cmd); $mergeCol->setInitialized(true); } else { $mergeCol->setDocumentManager($this->dm); } $mergeCol->setOwner($managedCopy, $assoc2); $mergeCol->setDirty(true); // mark for dirty checking $prop->setValue($managedCopy, $mergeCol); } } if ($class->isChangeTrackingNotify()) { // Just treat all properties as changed, there is no other choice. $this->propertyChanged($managedCopy, $name, null, $prop->getValue($managedCopy)); } } if ($class->isChangeTrackingDeferredExplicit()) { $this->scheduleForDirtyCheck($document); } } if ($prevManagedCopy !== null) { $assocField = $assoc->sourceFieldName; $prevClass = $this->dm->getClassMetadata(get_class($prevManagedCopy)); if ($assoc->isOneToOne()) { $prevClass->reflFields[$assocField]->setValue($prevManagedCopy, $managedCopy); } else { $prevClass->reflFields[$assocField]->getValue($prevManagedCopy)->unwrap()->add($managedCopy); if ($assoc->isOneToMany()) { $class->reflFields[$assoc->mappedBy]->setValue($managedCopy, $prevManagedCopy); } } } // Mark the managed copy visited as well $visited[spl_object_hash($managedCopy)] = true; $this->cascadeMerge($document, $managedCopy, $visited); return $managedCopy; }
/** * Computes the changes that happened to a single document. * * Modifies/populates the following properties: * * {@link _originalDocumentData} * If the document is NEW or MANAGED but not yet fully persisted (only has an id) * then it was not fetched from the database and therefore we have no original * document data yet. All of the current document data is stored as the original document data. * * {@link _documentChangeSets} * The changes detected on all properties of the document are stored there. * A change is a tuple array where the first entry is the old value and the second * entry is the new value of the property. Changesets are used by persisters * to INSERT/UPDATE the persistent document state. * * {@link _documentUpdates} * If the document is already fully MANAGED (has been fetched from the database before) * and any changes to its properties are detected, then a reference to the document is stored * there to mark it for an update. * * @param ClassMetadata $class The class descriptor of the document. * @param object $document The document for which to compute the changes. */ public function computeChangeSet(Mapping\ClassMetadata $class, $document) { if (!$class->isInheritanceTypeNone()) { $class = $this->_dm->getClassMetadata(get_class($document)); } $oid = spl_object_hash($document); $actualData = array(); foreach ($class->reflFields as $name => $refProp) { if (!$class->isIdentifier($name)) { $actualData[$name] = $refProp->getValue($document); } if ($class->isCollectionValuedReference($name) && $actualData[$name] !== null && !$actualData[$name] instanceof PersistentCollection) { // If $actualData[$name] is not a Collection then use an ArrayCollection. if (!$actualData[$name] instanceof Collection) { $actualData[$name] = new ArrayCollection($actualData[$name]); } $mapping = $class->fieldMappings[$name]; // Inject PersistentCollection $coll = new PersistentCollection($this->_dm, $this->_dm->getClassMetadata($mapping['targetDocument']), $actualData[$name]); $coll->setOwner($document, $mapping); $coll->setDirty(!$coll->isEmpty()); $class->reflFields[$name]->setValue($document, $coll); $actualData[$name] = $coll; } } if (!isset($this->_originalDocumentData[$oid])) { // Document is either NEW or MANAGED but not yet fully persisted (only has an id). // These result in an INSERT. $this->_originalDocumentData[$oid] = $actualData; $this->_documentChangeSets[$oid] = array_map(function ($e) { return array(null, $e); }, $actualData); } else { // Document is "fully" MANAGED: it was already fully persisted before // and we have a copy of the original data $originalData = $this->_originalDocumentData[$oid]; $changeSet = array(); $documentIsDirty = false; foreach ($actualData as $propName => $actualValue) { $orgValue = isset($originalData[$propName]) ? $originalData[$propName] : null; if (is_object($orgValue)) { if ($orgValue instanceof PersistentCollection) { $orgValue = $orgValue->getSnapshot(); } if ($actualValue instanceof PersistentCollection) { $actualValue = $actualValue->toArray(); } if ($orgValue !== $actualValue) { $changeSet[$propName] = array($orgValue, $actualValue); } } elseif ($orgValue != $actualValue || $orgValue === null ^ $actualValue === null) { $changeSet[$propName] = array($orgValue, $actualValue); } if (isset($changeSet[$propName])) { if (isset($class->fieldMappings[$propName]['reference'])) { $mapping = $class->fieldMappings[$propName]; if ($mapping['type'] === 'one') { $documentIsDirty = true; if ($actualValue === null) { $this->scheduleOrphanRemoval($orgValue); } } } else { $documentIsDirty = true; } } } if ($changeSet) { $this->_documentChangeSets[$oid] = $changeSet; $this->_originalDocumentData[$oid] = $actualData; if ($documentIsDirty) { $this->_documentUpdates[$oid] = $document; } } } // Look for changes in references of the document foreach ($class->fieldMappings as $mapping) { if (!isset($mapping['reference'])) { continue; } $val = $class->reflFields[$mapping['fieldName']]->getValue($document); if ($val !== null) { $this->_computeAssociationChanges($mapping, $val); } } }
/** * {@inheritdoc} */ public function setDirty($dirty) { $this->collection->setDirty($dirty); }
/** * Computes the changes that happened to a single document. * * Modifies/populates the following properties: * * {@link _originalDocumentData} * If the document is NEW or MANAGED but not yet fully persisted (only has an id) * then it was not fetched from the database and therefore we have no original * document data yet. All of the current document data is stored as the original document data. * * {@link _documentChangeSets} * The changes detected on all properties of the document are stored there. * A change is a tuple array where the first entry is the old value and the second * entry is the new value of the property. Changesets are used by persisters * to INSERT/UPDATE the persistent document state. * * {@link _documentUpdates} * If the document is already fully MANAGED (has been fetched from the database before) * and any changes to its properties are detected, then a reference to the document is stored * there to mark it for an update. * * @param object $parentDocument The top most parent document of the document we are computing. * @param ClassMetadata $class The class descriptor of the document. * @param object $document The document for which to compute the changes. */ public function computeChangeSet($parentDocument, Mapping\ClassMetadata $class, $document) { if (!$class->isInheritanceTypeNone()) { $class = $this->dm->getClassMetadata(get_class($document)); } $oid = spl_object_hash($document); $parentOid = spl_object_hash($parentDocument); $actualData = array(); foreach ($class->reflFields as $name => $refProp) { $mapping = $class->fieldMappings[$name]; if (!$class->isIdentifier($name) || $class->getAllowCustomID()) { $actualData[$name] = $class->getFieldValue($document, $mapping['fieldName']); } if (($class->isCollectionValuedReference($name) || $class->isCollectionValuedEmbed($name)) && $actualData[$name] !== null && !$actualData[$name] instanceof PersistentCollection) { // If $actualData[$name] is not a Collection then use an ArrayCollection. if (!$actualData[$name] instanceof Collection) { $actualData[$name] = new ArrayCollection($actualData[$name]); } // Inject PersistentCollection if ($class->isCollectionValuedReference($name)) { $coll = new PersistentCollection($actualData[$name], $this->dm); } else { $coll = new PersistentCollection($actualData[$name]); } $coll->setOwner($document, $mapping); $coll->setDirty(!$coll->isEmpty()); $class->reflFields[$name]->setValue($document, $coll); $actualData[$name] = $coll; } elseif ($class->isSingleValuedEmbed($name) && is_object($actualData[$name])) { $embeddedDocument = $actualData[$name]; $embeddedMetadata = $this->dm->getClassMetadata(get_class($embeddedDocument)); $actualData[$name] = array(); foreach ($embeddedMetadata->fieldMappings as $mapping) { $actualData[$name][$mapping['fieldName']] = $embeddedMetadata->getFieldValue($embeddedDocument, $mapping['fieldName']); } $actualData[$name]['originalObject'] = $embeddedDocument; } } if (!isset($this->originalDocumentData[$oid])) { // Document is either NEW or MANAGED but not yet fully persisted (only has an id). // These result in an INSERT. $this->originalDocumentData[$oid] = $actualData; $changeSet = array(); foreach ($actualData as $propName => $actualValue) { $changeSet[$propName] = array(null, $actualValue); } $this->documentChangeSets[$oid] = $changeSet; } else { // Document is "fully" MANAGED: it was already fully persisted before // and we have a copy of the original data $originalData = $this->originalDocumentData[$oid]; $isChangeTrackingNotify = $class->isChangeTrackingNotify(); $changeSet = $isChangeTrackingNotify ? $this->documentChangeSets[$oid] : array(); foreach ($actualData as $propName => $actualValue) { $orgValue = isset($originalData[$propName]) ? $originalData[$propName] : null; if ($orgValue instanceof PersistentCollection) { $orgValue = $orgValue->getSnapshot(); } if ($actualValue instanceof PersistentCollection) { $actualValue = $actualValue->toArray(); } if (isset($class->fieldMappings[$propName]['embedded']) && $class->fieldMappings[$propName]['type'] === 'one' && $orgValue !== $actualValue) { if (is_object($orgValue)) { $embeddedOid = spl_object_hash($orgValue); $orgValue = isset($this->originalDocumentData[$embeddedOid]) ? $this->originalDocumentData[$embeddedOid] : $orgValue; } $changeSet[$propName] = array($orgValue, $actualValue); } else { if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['type'] === 'one' && $orgValue !== $actualValue) { if (is_object($orgValue)) { $referenceOid = spl_object_hash($orgValue); $orgValue = isset($this->originalDocumentData[$referenceOid]) ? $this->originalDocumentData[$referenceOid] : $orgValue; } $changeSet[$propName] = array($orgValue, $actualValue); } else { if ($isChangeTrackingNotify) { continue; } else { if (isset($class->fieldMappings[$propName]['type']) && $class->fieldMappings[$propName]['type'] === 'many') { $changeSet[$propName] = array($orgValue, $actualValue); } else { if (is_object($orgValue) && $orgValue !== $actualValue) { $changeSet[$propName] = array($orgValue, $actualValue); } else { if ($orgValue != $actualValue || $orgValue === null ^ $actualValue === null) { $changeSet[$propName] = array($orgValue, $actualValue); } } } } } } } if ($changeSet) { $this->documentChangeSets[$oid] = $changeSet; $this->originalDocumentData[$oid] = $actualData; if (!$class->isEmbeddedDocument) { $this->documentUpdates[$oid] = $document; } } } // Look for changes in references of the document foreach ($class->fieldMappings as $mapping) { if (isset($mapping['reference']) || isset($mapping['embedded'])) { $val = $class->reflFields[$mapping['fieldName']]->getValue($document); if ($val !== null) { $this->computeAssociationChanges($parentDocument, $mapping, $val); } } } }
/** * Get a documents actual data, flattening all the objects to arrays. * * @param object $document * @return array */ public function getDocumentActualData($document) { $class = $this->dm->getClassMetadata(get_class($document)); $actualData = array(); foreach ($class->reflFields as $name => $refProp) { $mapping = $class->fieldMappings[$name]; // Skip identifiers if custom ones are not allowed if ($class->isIdentifier($name) && !$class->getAllowCustomID()) { continue; } $origValue = $class->getFieldValue($document, $mapping['fieldName']); if (($class->isCollectionValuedReference($name) || $class->isCollectionValuedEmbed($name)) && $origValue !== null && !$origValue instanceof PersistentCollection) { // If $actualData[$name] is not a Collection then use an ArrayCollection. if (!$origValue instanceof Collection) { $value = new ArrayCollection($origValue); } else { $value = $origValue; } // Inject PersistentCollection if ($class->isCollectionValuedReference($name)) { $coll = new PersistentCollection($value, $this->dm); } else { $coll = new PersistentCollection($value); } $coll->setOwner($document, $mapping); $coll->setDirty(!$coll->isEmpty()); $class->reflFields[$name]->setValue($document, $coll); } // We need to flatten the embedded documents so they are just arrays of // data instead of the actual objects. This is necessary to maintain all the old // values. if ($class->isCollectionValuedEmbed($name) && $origValue) { $embeddedDocuments = $origValue; $actualData[$name] = array(); foreach ($embeddedDocuments as $key => $embeddedDocument) { $actualData[$name][$key] = $this->getDocumentActualData($embeddedDocument); $actualData[$name][$key]['originalObject'] = $embeddedDocument; } } elseif ($class->isSingleValuedEmbed($name) && is_object($origValue)) { $embeddedDocument = $origValue; $actualData[$name] = $this->getDocumentActualData($embeddedDocument); $actualData[$name]['originalObject'] = $embeddedDocument; } else { $actualData[$name] = $origValue; } } return $actualData; }