/** * @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()); }
/** * 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; }
private function loadReferenceManyCollectionOwningSide(PersistentCollection $collection) { $hints = $collection->getHints(); $mapping = $collection->getMapping(); $groupedIds = array(); $sorted = isset($mapping['sort']) && $mapping['sort']; foreach ($collection->getMongoData() as $key => $reference) { if (isset($mapping['simple']) && $mapping['simple']) { $className = $mapping['targetDocument']; $mongoId = $reference; } else { $className = $this->uow->getClassNameForAssociation($mapping, $reference); $mongoId = $reference['$id']; } $id = $this->dm->getClassMetadata($className)->getPHPIdentifierValue($mongoId); // create a reference to the class and id $reference = $this->dm->getReference($className, $id); // no custom sort so add the references right now in the order they are embedded if (!$sorted) { if (CollectionHelper::isHash($mapping['strategy'])) { $collection->set($key, $reference); } else { $collection->add($reference); } } // only query for the referenced object if it is not already initialized or the collection is sorted if ($reference instanceof Proxy && !$reference->__isInitialized__ || $sorted) { $groupedIds[$className][] = $mongoId; } } 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) { $document = $this->uow->getById($documentData['_id'], $class); $data = $this->hydratorFactory->hydrate($document, $documentData); $this->uow->setOriginalDocumentData($document, $data); $document->__isInitialized__ = true; if ($sorted) { $collection->add($document); } } } }
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); } }
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); } }
/** * {@inheritdoc} */ public function add($value) { return $this->collection->add($value); }
/** * @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 testOffsetSetIsForwarded() { $collection = $this->getMockCollection(); $collection->expects($this->exactly(2))->method('offsetSet'); $pcoll = new PersistentCollection($collection, $this->getMockDocumentManager(), $this->getMockUnitOfWork()); $pcoll[] = 1; $pcoll[1] = 2; $collection->expects($this->once())->method('add'); $pcoll->add(3); $collection->expects($this->once())->method('set'); $pcoll->set(3, 4); }
/** * 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); } } }
/** * 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; }