Since: 1.0
Author: Jonathan H. Wage (jonwage@gmail.com)
Author: Roman Borschel (roman@code-factory.org)
Inheritance: implements Doctrine\Common\Collections\Collection
 public function testSlice()
 {
     list($start, $limit) = array(0, 25);
     $collection = $this->getMockCollection();
     $collection->expects($this->once())->method('slice')->with($start, $limit)->will($this->returnValue(true));
     $dm = $this->getMockDocumentManager();
     $uow = $this->getMockUnitOfWork();
     $pCollection = new PersistentCollection($collection, $dm, $uow, '$');
     $pCollection->slice($start, $limit);
 }
 /**
  * @param array $expected
  * @param array $snapshot
  * @param \Closure $callback
  *
  * @dataProvider dataGetDeletedDocuments
  */
 public function testGetDeletedDocuments($expected, $snapshot, \Closure $callback)
 {
     $collection = new PersistentCollection(new ArrayCollection(), $this->getMockDocumentManager(), $this->getMockUnitOfWork());
     foreach ($snapshot as $item) {
         $collection->add($item);
     }
     $collection->takeSnapshot();
     $callback($collection);
     $this->assertSame($expected, $collection->getDeletedDocuments());
 }
 private function loadReferenceManyWithRepositoryMethod(PersistentCollection $collection)
 {
     $mapping = $collection->getMapping();
     $cursor = $this->dm->getRepository($mapping['targetDocument'])->$mapping['repositoryMethod']();
     if ($mapping['sort']) {
         $cursor->sort($mapping['sort']);
     }
     if ($mapping['limit']) {
         $cursor->limit($mapping['limit']);
     }
     if ($mapping['skip']) {
         $cursor->skip($mapping['skip']);
     }
     foreach ($cursor as $document) {
         $collection->add($document);
     }
 }
 /**
  * Returns the collection representation to be stored and unschedules it afterwards.
  *
  * @param PersistentCollection $coll
  * @param bool $includeNestedCollections
  * @return array
  */
 public function prepareAssociatedCollectionValue(PersistentCollection $coll, $includeNestedCollections = false)
 {
     $mapping = $coll->getMapping();
     $pb = $this;
     $callback = isset($mapping['embedded']) ? function ($v) use($pb, $mapping, $includeNestedCollections) {
         return $pb->prepareEmbeddedDocumentValue($mapping, $v, $includeNestedCollections);
     } : function ($v) use($pb, $mapping) {
         return $pb->prepareReferencedDocumentValue($mapping, $v);
     };
     $setData = $coll->map($callback)->toArray();
     if (CollectionHelper::isList($mapping['strategy'])) {
         $setData = array_values($setData);
     }
     $this->uow->unscheduleCollectionDeletion($coll);
     $this->uow->unscheduleCollectionUpdate($coll);
     return $setData;
 }
Example #5
0
 /**
  * @param PersistentCollection $collection
  *
  * @return Cursor
  */
 public function createReferenceManyWithRepositoryMethodCursor(PersistentCollection $collection)
 {
     $hints = $collection->getHints();
     $mapping = $collection->getMapping();
     $cursor = $this->dm->getRepository($mapping['targetDocument'])->{$mapping}['repositoryMethod']($collection->getOwner());
     if (isset($mapping['sort'])) {
         $cursor->sort($mapping['sort']);
     }
     if (isset($mapping['limit'])) {
         $cursor->limit($mapping['limit']);
     }
     if (isset($mapping['skip'])) {
         $cursor->skip($mapping['skip']);
     }
     if (!empty($hints[Query::HINT_SLAVE_OKAY])) {
         $cursor->slaveOkay(true);
     }
     if (!empty($hints[Query::HINT_READ_PREFERENCE])) {
         $cursor->setReadPreference($hints[Query::HINT_READ_PREFERENCE], $hints[Query::HINT_READ_PREFERENCE_TAGS]);
     }
     return $cursor;
 }
Example #6
0
 /**
  * Executes a merge operation on a document.
  *
  * @param object      $document
  * @param array       $visited
  * @param object|null $prevManagedCopy
  * @param array|null  $assoc
  *
  * @return object The managed copy of the document.
  *
  * @throws InvalidArgumentException If the document instance is NEW.
  * @throws LockException If the entity uses optimistic locking through a
  *                       version attribute and the version check against the
  *                       managed copy fails.
  */
 private function doMerge($document, array &$visited, $prevManagedCopy = null, $assoc = null)
 {
     $oid = spl_object_hash($document);
     if (isset($visited[$oid])) {
         return $visited[$oid];
         // 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 round trip 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.
      */
     $managedCopy = $document;
     if ($this->getDocumentState($document, self::STATE_DETACHED) !== self::STATE_MANAGED) {
         if ($document instanceof Proxy && !$document->__isInitialized()) {
             $document->__load();
         }
         // Try to look the document up in the identity map.
         $id = $class->isEmbeddedDocument ? null : $class->getIdentifierObject($document);
         if ($id === null) {
             // If there is no identifier, it is actually NEW.
             $managedCopy = $class->newInstance();
             $this->persistNew($class, $managedCopy);
         } else {
             $managedCopy = $this->tryGetById($id, $class);
             if ($managedCopy) {
                 // We have the document in memory already, just make sure it is not removed.
                 if ($this->getDocumentState($managedCopy) === self::STATE_REMOVED) {
                     throw new \InvalidArgumentException('Removed entity detected during merge. Cannot 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
                 $managedCopy = $class->newInstance();
                 $class->setIdentifierValue($managedCopy, $id);
                 $this->persistNew($class, $managedCopy);
             } else {
                 if ($managedCopy instanceof Proxy && !$managedCopy->__isInitialized__) {
                     $managedCopy->__load();
                 }
             }
         }
         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($document, $documentVersion, $managedCopyVersion);
             }
         }
         // Merge state of $document into existing (managed) document
         foreach ($class->reflClass->getProperties() as $prop) {
             $name = $prop->name;
             $prop->setAccessible(true);
             if (!isset($class->associationMappings[$name])) {
                 if (!$class->isIdentifier($name)) {
                     $prop->setValue($managedCopy, $prop->getValue($document));
                 }
             } else {
                 $assoc2 = $class->associationMappings[$name];
                 if ($assoc2['type'] === 'one') {
                     $other = $prop->getValue($document);
                     if ($other === null) {
                         $prop->setValue($managedCopy, null);
                     } elseif ($other instanceof Proxy && !$other->__isInitialized__) {
                         // Do not merge fields marked lazy that have not been fetched
                         continue;
                     } elseif (!$assoc2['isCascadeMerge']) {
                         if ($this->getDocumentState($other) === self::STATE_DETACHED) {
                             $targetDocument = isset($assoc2['targetDocument']) ? $assoc2['targetDocument'] : get_class($other);
                             /* @var $targetClass \Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo */
                             $targetClass = $this->dm->getClassMetadata($targetDocument);
                             $relatedId = $targetClass->getIdentifierObject($other);
                             if ($targetClass->subClasses) {
                                 $other = $this->dm->find($targetClass->name, $relatedId);
                             } else {
                                 $other = $this->dm->getProxyFactory()->getProxy($assoc2['targetDocument'], array($targetClass->identifier => $relatedId));
                                 $this->registerManaged($other, $relatedId, array());
                             }
                         }
                         $prop->setValue($managedCopy, $other);
                     }
                 } 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;
                     }
                     $managedCol = $prop->getValue($managedCopy);
                     if (!$managedCol) {
                         $managedCol = new PersistentCollection(new ArrayCollection(), $this->dm, $this);
                         $managedCol->setOwner($managedCopy, $assoc2);
                         $prop->setValue($managedCopy, $managedCol);
                         $this->originalDocumentData[$oid][$name] = $managedCol;
                     }
                     /* Note: do not process association's target documents.
                      * They will be handled during the cascade. Initialize
                      * and, if necessary, clear $managedCol for now.
                      */
                     if ($assoc2['isCascadeMerge']) {
                         $managedCol->initialize();
                         // If $managedCol differs from the merged collection, clear and set dirty
                         if (!$managedCol->isEmpty() && $managedCol !== $mergeCol) {
                             $managedCol->unwrap()->clear();
                             $managedCol->setDirty(true);
                             if ($assoc2['isOwningSide'] && $class->isChangeTrackingNotify()) {
                                 $this->scheduleForDirtyCheck($managedCopy);
                             }
                         }
                     }
                 }
             }
             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['fieldName'];
         $prevClass = $this->dm->getClassMetadata(get_class($prevManagedCopy));
         if ($assoc['type'] === 'one') {
             $prevClass->reflFields[$assocField]->setValue($prevManagedCopy, $managedCopy);
         } else {
             $prevClass->reflFields[$assocField]->getValue($prevManagedCopy)->add($managedCopy);
             if ($assoc['type'] === 'many' && isset($assoc['mappedBy'])) {
                 $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;
 }
Example #7
0
 /**
  * Marks the PersistentCollection's top-level owner as having a relation to
  * a collection scheduled for update or deletion.
  *
  * If the owner is not scheduled for any lifecycle action, it will be
  * scheduled for update to ensure that versioning takes place if necessary.
  *
  * If the collection is nested within atomic collection, it is immediately
  * unscheduled and atomic one is scheduled for update instead. This makes
  * calculating update data way easier.
  * 
  * @param PersistentCollection $coll
  */
 private function scheduleCollectionOwner(PersistentCollection $coll)
 {
     $document = $this->getOwningDocument($coll->getOwner());
     $this->hasScheduledCollections[spl_object_hash($document)][spl_object_hash($coll)] = $coll;
     if ($document !== $coll->getOwner()) {
         $parent = $coll->getOwner();
         while (null !== ($parentAssoc = $this->getParentAssociation($parent))) {
             list($mapping, $parent, ) = $parentAssoc;
         }
         if (isset($mapping['strategy']) && CollectionHelper::isAtomic($mapping['strategy'])) {
             $class = $this->dm->getClassMetadata(get_class($document));
             $atomicCollection = $class->getFieldValue($document, $mapping['fieldName']);
             $this->scheduleCollectionUpdate($atomicCollection);
             $this->unscheduleCollectionDeletion($coll);
             $this->unscheduleCollectionUpdate($coll);
         }
     }
     if (!$this->isDocumentScheduled($document)) {
         $this->scheduleForUpdate($document);
     }
 }
Example #8
0
 /**
  * @param PersistentCollection $collection
  *
  * @return CursorInterface
  */
 public function createReferenceManyWithRepositoryMethodCursor(PersistentCollection $collection)
 {
     $hints = $collection->getHints();
     $mapping = $collection->getMapping();
     $repositoryMethod = $mapping['repositoryMethod'];
     $cursor = $this->dm->getRepository($mapping['targetDocument'])->{$repositoryMethod}($collection->getOwner());
     if (!$cursor instanceof CursorInterface) {
         throw new \BadMethodCallException("Expected repository method {$repositoryMethod} to return a CursorInterface");
     }
     if (isset($mapping['sort'])) {
         $cursor->sort($mapping['sort']);
     }
     if (isset($mapping['limit'])) {
         $cursor->limit($mapping['limit']);
     }
     if (isset($mapping['skip'])) {
         $cursor->skip($mapping['skip']);
     }
     if (!empty($hints[Query::HINT_SLAVE_OKAY])) {
         $cursor->slaveOkay(true);
     }
     if (!empty($hints[Query::HINT_READ_PREFERENCE])) {
         $cursor->setReadPreference($hints[Query::HINT_READ_PREFERENCE], $hints[Query::HINT_READ_PREFERENCE_TAGS]);
     }
     return $cursor;
 }
Example #9
0
 private function doGenericHydration(ClassMetadata $metadata, $document, $data)
 {
     foreach ($metadata->fieldMappings as $mapping) {
         // Find the raw value. It may be in one of the mapped alsoLoadFields.
         $found = false;
         if (isset($mapping['alsoLoadFields']) && $mapping['alsoLoadFields']) {
             foreach ($mapping['alsoLoadFields'] as $name) {
                 if (isset($data[$name])) {
                     $rawValue = $data[$name];
                     $found = true;
                     break;
                 }
             }
         }
         // If nothing then lets get it from the default mapping field name
         if ($found === false) {
             $rawValue = isset($data[$mapping['name']]) ? $data[$mapping['name']] : null;
         }
         $value = null;
         // Prepare the different types of mapped values converting them from the MongoDB
         // types to the portable Doctrine types.
         // @Field
         if (!isset($mapping['association'])) {
             $value = Type::getType($mapping['type'])->convertToPHPValue($rawValue);
             // @ReferenceOne
         } elseif ($mapping['association'] === ClassMetadata::REFERENCE_ONE) {
             $reference = $rawValue;
             if ($reference === null || !isset($reference[$this->cmd . 'id'])) {
                 continue;
             }
             $className = $this->dm->getClassNameFromDiscriminatorValue($mapping, $reference);
             $targetMetadata = $this->dm->getClassMetadata($className);
             $id = $targetMetadata->getPHPIdentifierValue($reference[$this->cmd . 'id']);
             $value = $this->dm->getReference($className, $id);
             // @ReferenceMany and @EmbedMany
         } elseif ($mapping['association'] === ClassMetadata::REFERENCE_MANY || $mapping['association'] === ClassMetadata::EMBED_MANY) {
             $value = new PersistentCollection(new ArrayCollection(), $this->dm, $this->unitOfWork, $this->cmd);
             $value->setOwner($document, $mapping);
             $value->setInitialized(false);
             if ($rawValue) {
                 $value->setMongoData($rawValue);
             }
             // @EmbedOne
         } elseif ($mapping['association'] === ClassMetadata::EMBED_ONE) {
             if ($rawValue === null) {
                 continue;
             }
             $embeddedDocument = $rawValue;
             $className = $this->dm->getClassNameFromDiscriminatorValue($mapping, $embeddedDocument);
             $embeddedMetadata = $this->dm->getClassMetadata($className);
             $value = $embeddedMetadata->newInstance();
             $embeddedHydratedData = $this->hydrate($value, $embeddedDocument);
             $this->unitOfWork->registerManaged($value, null, $embeddedHydratedData);
             $this->unitOfWork->setParentAssociation($value, $mapping, $document, $mapping['name']);
         }
         unset($data[$mapping['name']]);
         // Hydrate the prepared value to the document
         if ($value !== null) {
             $metadata->reflFields[$mapping['fieldName']]->setValue($document, $value);
             $data[$mapping['fieldName']] = $value;
         }
     }
     return $data;
 }
 /**
  * @param PersistentCollection $collection
  * @param array $groupedIds
  *
  * @throws \Doctrine\ODM\MongoDB\MongoDBException
  */
 private function loadActualDataForSortedReferenceManyCollectionByIds(PersistentCollection $collection, array $groupedIds)
 {
     $mapping = $collection->getMapping();
     $hints = $collection->getHints();
     foreach ($groupedIds as $className => $ids) {
         $class = $this->dm->getClassMetadata($className);
         $mongoCollection = $this->dm->getDocumentCollection($className);
         $criteria = $this->cm->merge(array('_id' => array('$in' => array_values($ids))), $this->dm->getFilterCollection()->getFilterCriteria($class), isset($mapping['criteria']) ? $mapping['criteria'] : array());
         $criteria = $this->uow->getDocumentPersister($className)->prepareQueryOrNewObj($criteria);
         $cursor = $mongoCollection->find($criteria);
         if (isset($mapping['sort'])) {
             $cursor->sort($mapping['sort']);
         }
         if (isset($mapping['limit'])) {
             $cursor->limit($mapping['limit']);
         }
         if (isset($mapping['skip'])) {
             $cursor->skip($mapping['skip']);
         }
         if (!empty($hints[Query::HINT_SLAVE_OKAY])) {
             $cursor->slaveOkay(true);
         }
         if (!empty($hints[Query::HINT_READ_PREFERENCE])) {
             $cursor->setReadPreference($hints[Query::HINT_READ_PREFERENCE], $hints[Query::HINT_READ_PREFERENCE_TAGS]);
         }
         $documents = $cursor->toArray(false);
         foreach ($documents as $documentData) {
             $docId = $documentData['_id'];
             $document = $this->uow->getById($docId, $class);
             $data = $this->hydratorFactory->hydrate($document, $documentData);
             $this->uow->setOriginalDocumentData($document, $data);
             $document->__isInitialized__ = true;
             $collection->add($document);
         }
     }
 }
 private function loadReferenceManyCollection(PersistentCollection $collection)
 {
     $mapping = $collection->getMapping();
     $cmd = $this->cmd;
     $groupedIds = array();
     foreach ($collection->getMongoData() as $reference) {
         $className = $this->dm->getClassNameFromDiscriminatorValue($mapping, $reference);
         $mongoId = $reference[$cmd . 'id'];
         $id = (string) $mongoId;
         $reference = $this->dm->getReference($className, $id);
         $collection->add($reference);
         if ($reference instanceof Proxy && !$reference->__isInitialized__) {
             if (!isset($groupedIds[$className])) {
                 $groupedIds[$className] = array();
             }
             $groupedIds[$className][] = $mongoId;
         }
     }
     foreach ($groupedIds as $className => $ids) {
         $class = $this->dm->getClassMetadata($className);
         $mongoCollection = $this->dm->getDocumentCollection($className);
         $data = $mongoCollection->find(array('_id' => array($cmd . 'in' => $ids)));
         foreach ($data as $documentData) {
             $document = $this->uow->getById((string) $documentData['_id'], $class->rootDocumentName);
             $data = $this->hydratorFactory->hydrate($document, $documentData);
             $this->uow->setOriginalDocumentData($document, $data);
         }
     }
 }
 public function testIsEmptyUsesCountWhenCollectionIsNotInitialized()
 {
     $collection = $this->getMockCollection();
     $collection->expects($this->never())->method('isEmpty');
     $collection->expects($this->once())->method('count')->willReturn(0);
     $pcoll = new PersistentCollection($collection, $this->getMockDocumentManager(), $this->getMockUnitOfWork());
     $pcoll->setInitialized(false);
     $this->assertTrue($pcoll->isEmpty());
 }
Example #13
0
 /**
  * Hydrate array of MongoDB document data into the given document object.
  *
  * @param object $document  The document object to hydrate the data into.
  * @param array $data The array of document data.
  * @return array $values The array of hydrated values.
  */
 public function hydrate($document, &$data)
 {
     $metadata = $this->dm->getClassMetadata(get_class($document));
     if (isset($metadata->alsoLoadMethods)) {
         foreach ($metadata->alsoLoadMethods as $fieldName => $method) {
             if (isset($data[$fieldName])) {
                 $document->{$method}($data[$fieldName]);
             }
         }
     }
     foreach ($metadata->fieldMappings as $mapping) {
         if (isset($mapping['alsoLoadFields'])) {
             $rawValue = null;
             $names = isset($mapping['alsoLoadFields']) ? $mapping['alsoLoadFields'] : array();
             array_unshift($names, $mapping['name']);
             foreach ($names as $name) {
                 if (isset($data[$name])) {
                     $rawValue = $data[$name];
                     break;
                 }
             }
         } else {
             $rawValue = isset($data[$mapping['name']]) ? $data[$mapping['name']] : null;
         }
         if ($rawValue === null) {
             continue;
         }
         $value = null;
         // Hydrate embedded
         if (isset($mapping['embedded'])) {
             if ($mapping['type'] === 'one') {
                 $embeddedDocument = $rawValue;
                 $className = $this->dm->getClassNameFromDiscriminatorValue($mapping, $embeddedDocument);
                 $embeddedMetadata = $this->dm->getClassMetadata($className);
                 $value = $embeddedMetadata->newInstance();
                 $this->hydrate($value, $embeddedDocument);
                 $this->dm->getUnitOfWork()->registerManagedEmbeddedDocument($value, $embeddedDocument);
             } elseif ($mapping['type'] === 'many') {
                 $embeddedDocuments = $rawValue;
                 $coll = new PersistentCollection(new ArrayCollection());
                 foreach ($embeddedDocuments as $embeddedDocument) {
                     $className = $this->dm->getClassNameFromDiscriminatorValue($mapping, $embeddedDocument);
                     $embeddedMetadata = $this->dm->getClassMetadata($className);
                     $embeddedDocumentObject = $embeddedMetadata->newInstance();
                     $this->hydrate($embeddedDocumentObject, $embeddedDocument);
                     $this->dm->getUnitOfWork()->registerManagedEmbeddedDocument($embeddedDocumentObject, $embeddedDocument);
                     $coll->add($embeddedDocumentObject);
                 }
                 $coll->setOwner($document, $mapping);
                 $coll->takeSnapshot();
                 $value = $coll;
             }
             // Hydrate reference
         } elseif (isset($mapping['reference'])) {
             $reference = $rawValue;
             if ($mapping['type'] === 'one' && isset($reference[$this->cmd . 'id'])) {
                 $className = $this->dm->getClassNameFromDiscriminatorValue($mapping, $reference);
                 $targetMetadata = $this->dm->getClassMetadata($className);
                 $id = $targetMetadata->getPHPIdentifierValue($reference[$this->cmd . 'id']);
                 $value = $this->dm->getReference($className, $id);
             } elseif ($mapping['type'] === 'many' && (is_array($reference) || $reference instanceof Collection)) {
                 $references = $reference;
                 $value = new PersistentCollection(new ArrayCollection(), $this->dm);
                 $value->setInitialized(false);
                 $value->setOwner($document, $mapping);
                 // Delay any hydration of reference objects until the collection is
                 // accessed and initialized for the first ime
                 $value->setReferences($references);
             }
             // Hydrate regular field
         } else {
             $value = Type::getType($mapping['type'])->convertToPHPValue($rawValue);
         }
         // Set hydrated field value to document
         if ($value !== null) {
             $data[$mapping['name']] = $value;
             $metadata->setFieldValue($document, $mapping['fieldName'], $value);
         }
     }
     // Set the document identifier
     if (isset($data['_id'])) {
         $metadata->setIdentifierValue($document, $data['_id']);
         $data[$metadata->identifier] = $data['_id'];
         unset($data['_id']);
     }
     return $document;
 }
    public function loadCollection(PersistentCollection $collection)
    {
        $mapping = $collection->getMapping();
        $cmd = $this->dm->getConfiguration()->getMongoCmd();
        $groupedIds = array();
        foreach ($collection->getReferences() as $reference) {
            $className = $this->dm->getClassNameFromDiscriminatorValue($mapping, $reference);
            $id = $reference[$cmd . 'id'];
            $reference = $this->dm->getReference($className, (string) $id);
            $collection->add($reference);
            if ($reference instanceof Proxy && ! $reference->__isInitialized__) {
                if ( ! isset($groupedIds[$className])) {
                    $groupedIds[$className] = array();
                }
                $groupedIds[$className][] = $id;
            }
        }

        foreach ($groupedIds as $className => $ids) {
            $mongoCollection = $this->dm->getDocumentCollection($className);
            $data = $mongoCollection->find(array('_id' => array($cmd . 'in' => $ids)));
            $hints = array(Builder::HINT_REFRESH => true);
            foreach ($data as $id => $documentData) {
                $document = $this->uow->getOrCreateDocument($className, $documentData, $hints);
            }
        }
    }
Example #15
0
 private function loadReferenceManyWithRepositoryMethod(PersistentCollection $collection)
 {
     $mapping = $collection->getMapping();
     $cursor = $this->dm->getRepository($mapping['targetDocument'])->{$mapping}['repositoryMethod']($collection->getOwner());
     if (isset($mapping['sort']) && $mapping['sort']) {
         $cursor->sort($mapping['sort']);
     }
     if (isset($mapping['limit']) && $mapping['limit']) {
         $cursor->limit($mapping['limit']);
     }
     if (isset($mapping['skip']) && $mapping['skip']) {
         $cursor->skip($mapping['skip']);
     }
     if (isset($hints[Query::HINT_SLAVE_OKAY])) {
         $cursor->slaveOkay(true);
     }
     $documents = $cursor->toArray();
     foreach ($documents as $document) {
         $collection->add($document);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function unwrap()
 {
     return $this->collection->unwrap();
 }
Example #17
0
 /**
  * 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 {
         // 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 dont 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) {
                         $mergeCol = new PersistentCollection($mergeCol, $this->dm, $this, $this->cmd);
                         $mergeCol->setInitialized(true);
                     }
                     $mergeCol->setOwner($managedCopy, $assoc2);
                     $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;
 }
 /**
  * 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, $mapping = null)
 {
     $class = $this->_dm->getClassMetadata(get_class($document));
     $id = $class->getIdentifierValues($document);
     if (!$id) {
         throw new \InvalidArgumentException('New document detected during merge.' . ' Persist the new document before merging.');
     }
     // MANAGED documents are ignored by the merge operation
     if ($this->getDocumentState($document, self::STATE_DETACHED) == self::STATE_MANAGED) {
         $managedCopy = $document;
     } else {
         // Try to look the document up in the identity map.
         $managedCopy = $this->tryGetById($id, $class->rootDocumentName);
         if ($managedCopy) {
             // We have the document in-memory already, just make sure its not removed.
             if ($this->getDocumentState($managedCopy) == self::STATE_REMOVED) {
                 throw new \InvalidArgumentException('Removed document detected during merge.' . ' Can not merge with a removed document.');
             }
         } else {
             // We need to fetch the managed copy in order to merge.
             $managedCopy = $this->_dm->find($class->name, $id);
         }
         if ($managedCopy === null) {
             throw new \InvalidArgumentException('New document detected during merge.' . ' Persist the new document before merging.');
         }
         // Merge state of $document into existing (managed) document
         foreach ($class->reflFields as $name => $prop) {
             if (!isset($class->fieldMappings[$name]['reference'])) {
                 $prop->setValue($managedCopy, $prop->getValue($document));
             } else {
                 $mapping2 = $class->fieldMappings[$name];
                 if ($mapping2['type'] === 'one') {
                     if (!$assoc2['isCascadeMerge']) {
                         $other = $class->reflFields[$name]->getValue($document);
                         //TODO: Just $prop->getValue($document)?
                         if ($other !== null) {
                             $targetClass = $this->_dm->getClassMetadata($mapping2['targetDocument']);
                             $id = $targetClass->getIdentifierValue($other);
                             $proxy = $this->_dm->getProxyFactory()->getProxy($mapping2['targetDocument'], $id);
                             $prop->setValue($managedCopy, $proxy);
                             $this->registerManaged($proxy, $id, array());
                         }
                     }
                 } else {
                     $coll = new PersistentCollection($this->_dm, $this->_dm->getClassMetadata($mapping2['targetDocument']), new ArrayCollection());
                     $coll->setOwner($managedCopy, $mapping2);
                     $coll->setInitialized($mapping2['isCascadeMerge']);
                     $prop->setValue($managedCopy, $coll);
                 }
             }
         }
     }
     if ($prevManagedCopy !== null) {
         $assocField = $mapping['fieldName'];
         $prevClass = $this->_dm->getClassMetadata(get_class($prevManagedCopy));
         if ($mapping['type'] === 'one') {
             $prevClass->reflFields[$assocField]->setValue($prevManagedCopy, $managedCopy);
         } else {
             $prevClass->reflFields[$assocField]->getValue($prevManagedCopy)->unwrap()->add($managedCopy);
         }
     }
     $this->_cascadeMerge($document, $managedCopy, $visited);
     return $managedCopy;
 }
Example #19
0
 /**
  * Hydrate array of MongoDB document data into the given document object
  * based on the mapping information provided in the ClassMetadata instance.
  *
  * @param ClassMetadata $metadata  The ClassMetadata instance for mapping information.
  * @param string $document  The document object to hydrate the data into.
  * @param array $data The array of document data.
  * @return array $values The array of hydrated values.
  */
 public function hydrate(ClassMetadata $metadata, $document, $data)
 {
     $values = array();
     foreach ($metadata->fieldMappings as $mapping) {
         $rawValue = $this->_getFieldValue($mapping, $document, $data);
         if (!isset($rawValue)) {
             continue;
         }
         if (isset($mapping['embedded'])) {
             $embeddedMetadata = $this->_dm->getClassMetadata($mapping['targetDocument']);
             $embeddedDocument = $embeddedMetadata->newInstance();
             if ($mapping['type'] === 'many') {
                 $documents = new ArrayCollection();
                 foreach ($rawValue as $docArray) {
                     $doc = clone $embeddedDocument;
                     $this->hydrate($embeddedMetadata, $doc, $docArray);
                     $documents->add($doc);
                 }
                 $metadata->setFieldValue($document, $mapping['fieldName'], $documents);
                 $value = $documents;
             } else {
                 $value = clone $embeddedDocument;
                 $this->hydrate($embeddedMetadata, $value, $rawValue);
                 $metadata->setFieldValue($document, $mapping['fieldName'], $value);
             }
         } elseif (isset($mapping['reference'])) {
             $targetMetadata = $this->_dm->getClassMetadata($mapping['targetDocument']);
             $targetDocument = $targetMetadata->newInstance();
             if ($mapping['type'] === 'one' && isset($rawValue[$this->_cmd . 'id'])) {
                 $id = $targetMetadata->getPHPIdentifierValue($rawValue[$this->_cmd . 'id']);
                 $proxy = $this->_dm->getReference($mapping['targetDocument'], $id);
                 $metadata->setFieldValue($document, $mapping['fieldName'], $proxy);
             } elseif ($mapping['type'] === 'many' && (is_array($rawValue) || $rawValue instanceof Collection)) {
                 $documents = new PersistentCollection($this->_dm, $targetMetadata, new ArrayCollection());
                 $documents->setInitialized(false);
                 foreach ($rawValue as $v) {
                     $id = $targetMetadata->getPHPIdentifierValue($v[$this->_cmd . 'id']);
                     $proxy = $this->_dm->getReference($mapping['targetDocument'], $id);
                     $documents->add($proxy);
                 }
                 $metadata->setFieldValue($document, $mapping['fieldName'], $documents);
             }
         } else {
             $value = Type::getType($mapping['type'])->convertToPHPValue($rawValue);
             $metadata->setFieldValue($document, $mapping['fieldName'], $value);
         }
         if (isset($value)) {
             $values[$mapping['fieldName']] = $value;
         }
     }
     if (isset($data['_id'])) {
         $metadata->setIdentifierValue($document, $data['_id']);
     }
     return $values;
 }
 /**
  * Marks the PersistentCollection's top-level owner as having a relation to
  * a collection scheduled for update or deletion.
  *
  * If the owner is not scheduled for any lifecycle action, it will be
  * scheduled for update to ensure that versioning takes place if necessary.
  * 
  * @param PersistentCollection $coll
  */
 private function scheduleCollectionOwner(PersistentCollection $coll)
 {
     $document = $coll->getOwner();
     $class = $this->dm->getClassMetadata(get_class($document));
     while ($class->isEmbeddedDocument) {
         list(, $document, ) = $this->getParentAssociation($document);
         $class = $this->dm->getClassMetadata(get_class($document));
     }
     $this->hasScheduledCollections[spl_object_hash($document)] = true;
     if (!$this->isDocumentScheduled($document)) {
         $this->scheduleForUpdate($document);
     }
 }
 /**
  * Gets the parent information for a given PersistentCollection. It will
  * retrieve the top-level persistent Document that the PersistentCollection
  * lives in. We can use this to issue queries when updating a
  * PersistentCollection that is multiple levels deep inside an embedded
  * document.
  *
  *     <code>
  *     list($path, $parent) = $this->getPathAndParent($coll)
  *     </code>
  *
  * @param PersistentCollection $coll
  * @return array $pathAndParent
  */
 private function getPathAndParent(PersistentCollection $coll)
 {
     $mapping = $coll->getMapping();
     $fields = array();
     $parent = $coll->getOwner();
     while (null !== ($association = $this->uow->getParentAssociation($parent))) {
         list($m, $owner, $field) = $association;
         if (isset($m['reference'])) {
             break;
         }
         $parent = $owner;
         $fields[] = $field;
     }
     $propertyPath = implode('.', array_reverse($fields));
     $path = $mapping['name'];
     if ($propertyPath) {
         $path = $propertyPath . '.' . $path;
     }
     return array($path, $parent);
 }
Example #22
0
 /**
  * Initializes (loads) an uninitialized persistent collection of a document.
  *
  * @param PeristentCollection $collection The collection to initialize.
  */
 public function loadCollection(PersistentCollection $collection)
 {
     $mapping = $collection->getMapping();
     $this->getDocumentPersister(get_class($collection->getOwner()))->loadCollection($collection);
 }
 /**
  * Adds identifiers from a PersistentCollection to $groupedIds.
  *
  * If the relation contains simple references, the mapping is assumed to
  * have a target document class defined. Without that, there is no way to
  * infer the class of the referenced documents.
  *
  * @param PersistentCollection $persistentCollection
  * @param array                $groupedIds
  */
 private function addManyReferences(PersistentCollection $persistentCollection, array &$groupedIds)
 {
     $mapping = $persistentCollection->getMapping();
     if (!empty($mapping['simple'])) {
         $className = $mapping['targetDocument'];
         $class = $this->dm->getClassMetadata($className);
     }
     foreach ($persistentCollection->getMongoData() as $reference) {
         if (!empty($mapping['simple'])) {
             $id = $reference;
         } else {
             $id = $reference['$id'];
             $className = $this->uow->getClassNameForAssociation($mapping, $reference);
             $class = $this->dm->getClassMetadata($className);
         }
         $document = $this->uow->tryGetById($id, $class);
         if (!$document || $document instanceof Proxy && !$document->__isInitialized()) {
             $id = $class->getPHPIdentifierValue($id);
             $groupedIds[$className][serialize($id)] = $id;
         }
     }
 }
    /**
     * Hydrate array of MongoDB document data into the given document object.
     *
     * @param object $document  The document object to hydrate the data into.
     * @param array $data The array of document data.
     * @return array $values The array of hydrated values.
     */
    public function hydrate($document, &$data)
    {
        $metadata = $this->dm->getClassMetadata(get_class($document));

        if (isset($metadata->lifecycleCallbacks[Events::preLoad])) {
            $args = array(&$data);
            $metadata->invokeLifecycleCallbacks(Events::preLoad, $document, $args);
        }
        if ($this->evm->hasListeners(Events::preLoad)) {
            $this->evm->dispatchEvent(Events::preLoad, new PreLoadEventArgs($document, $this->dm, $data));
        }

        if (isset($metadata->alsoLoadMethods)) {
            foreach ($metadata->alsoLoadMethods as $fieldName => $method) {
                if (isset($data[$fieldName])) {
                    $document->$method($data[$fieldName]);
                }
            }
        }
        foreach ($metadata->fieldMappings as $mapping) {
            if (isset($mapping['alsoLoadFields'])) {
                $rawValue = null;
                $names = isset($mapping['alsoLoadFields']) ? $mapping['alsoLoadFields'] : array();
                array_unshift($names, $mapping['name']);
                foreach ($names as $name) {
                    if (isset($data[$name])) {
                        $rawValue = $data[$name];
                        break;
                    }
                }
            } else {
                $rawValue = isset($data[$mapping['name']]) ? $data[$mapping['name']] : null;
            }
            $value = null;

            if (isset($mapping['embedded'])) {
                $uow = $this->dm->getUnitOfWork();
                if ($mapping['type'] === 'one') {
                    if ($rawValue === null) {
                        continue;
                    }
                    $embeddedDocument = $rawValue;
                    $className = $this->dm->getClassNameFromDiscriminatorValue($mapping, $embeddedDocument);
                    $embeddedMetadata = $this->dm->getClassMetadata($className);
                    $value = $embeddedMetadata->newInstance();

                    // unset a potential discriminator map field (unless it's a persisted property)
                    $discriminatorField = isset($mapping['discriminatorField']) ? $mapping['discriminatorField'] : '_doctrine_class_name';
                    if (!isset($embeddedMetadata->fieldMappings[$discriminatorField])) {
                        unset($embeddedDocument[$discriminatorField]);
                    }

                    $this->hydrate($value, $embeddedDocument);
                    $uow->registerManaged($value, null, $embeddedDocument);
                    $uow->setParentAssociation($value, $mapping, $document, $mapping['name']);
                } elseif ($mapping['type'] === 'many') {
                    $embeddedDocuments = $rawValue;
                    $coll = new PersistentCollection(new ArrayCollection(), $this->dm, $this->dm->getConfiguration());
                    if ($embeddedDocuments) {
                        foreach ($embeddedDocuments as $key => $embeddedDocument) {
                            $className = $this->dm->getClassNameFromDiscriminatorValue($mapping, $embeddedDocument);
                            $embeddedMetadata = $this->dm->getClassMetadata($className);
                            $embeddedDocumentObject = $embeddedMetadata->newInstance();

                            // unset a potential discriminator map field (unless it's a persisted property)
                            $discriminatorField = isset($mapping['discriminatorField']) ? $mapping['discriminatorField'] : '_doctrine_class_name';
                            if (!isset($embeddedMetadata->fieldMappings[$discriminatorField])) {
                                unset($embeddedDocument[$discriminatorField]);
                            }

                            $this->hydrate($embeddedDocumentObject, $embeddedDocument);
                            $uow->registerManaged($embeddedDocumentObject, null, $embeddedDocument);
                            $uow->setParentAssociation($embeddedDocumentObject, $mapping, $document, $mapping['name'].'.'.$key);
                            $coll->add($embeddedDocumentObject);
                        }
                    }
                    $coll->setOwner($document, $mapping);
                    $coll->takeSnapshot();
                    $value = $coll;
                }
            // Hydrate reference
            } elseif (isset($mapping['reference'])) {
                $reference = $rawValue;
                if ($mapping['type'] === 'one' && isset($reference[$this->cmd . 'id'])) {
                    if ($reference === null) {
                        continue;
                    }
                    $className = $this->dm->getClassNameFromDiscriminatorValue($mapping, $reference);
                    $targetMetadata = $this->dm->getClassMetadata($className);
                    $id = $targetMetadata->getPHPIdentifierValue($reference[$this->cmd . 'id']);
                    $value = $this->dm->getReference($className, $id);
                } elseif ($mapping['type'] === 'many' && (is_array($reference) || $reference instanceof Collection)) {
                    $references = $reference;
                    $value = new PersistentCollection(new ArrayCollection(), $this->dm, $this->dm->getConfiguration());
                    $value->setInitialized(false);
                    $value->setOwner($document, $mapping);

                    // Delay any hydration of reference objects until the collection is
                    // accessed and initialized for the first ime
                    $value->setReferences($references);
                }
            // Hydrate regular field
            } else {
                $value = Type::getType($mapping['type'])->convertToPHPValue($rawValue);
            }

            unset($data[$mapping['name']]);
            // Set hydrated field value to document
            if ($value !== null) {
                $metadata->setFieldValue($document, $mapping['fieldName'], $value);
                $data[$mapping['fieldName']] = $value;
            }
        }
        // Set the document identifier
        if (isset($data['_id'])) {
            $metadata->setIdentifierValue($document, $data['_id']);
            $data[$metadata->identifier] = Type::getType($metadata->fieldMappings[$metadata->identifier]['type'])->convertToPHPValue($data['_id']);
            unset($data['_id']);
        }

        if (isset($metadata->lifecycleCallbacks[Events::postLoad])) {
            $metadata->invokeLifecycleCallbacks(Events::postLoad, $document);
        }
        if ($this->evm->hasListeners(Events::postLoad)) {
            $this->evm->dispatchEvent(Events::postLoad, new LifecycleEventArgs($document, $this->dm));
        }

        return $document;
    }