/** @override */ public function current() { $current = $this->mongoCursor->current(); if ($current && $this->hydrate) { return $this->unitOfWork->getOrCreateDocument($this->class->name, $current, $this->hints); } return $current ? $current : null; }
/** @override */ public function getNext() { $next = parent::getNext(); if ($next && $this->hydrate) { return $this->unitOfWork->getOrCreateDocument($this->class->name, $next, $this->hints); } return $next ? $next : null; }
public function current() { $this->initialize(); $current = current($this->data); if ($current && $this->hydrate) { return $this->unitOfWork->getOrCreateDocument($this->class->name, $current, $this->hints); } return $current ? $current : null; }
/** * Returns the reference representation to be stored in mongodb or null if not applicable. * * @param ClassMetadata $class * @param Document $doc * @return array|null */ private function _prepareDocReference(ClassMetadata $class, $doc) { if (!is_object($doc)) { return $doc; } $id = $this->_uow->getDocumentIdentifier($doc); $ref = array('$ref' => $class->getCollection(), '$id' => $id, '$db' => $class->getDB()); return $ref; }
/** * Returns a DBRef array for the supplied document. * * @param mixed $document A document object * @param array $referenceMapping Mapping for the field the references the document * * @return array A DBRef array */ public function createDBRef($document, array $referenceMapping = null) { $class = $this->getClassMetadata(get_class($document)); $id = $this->unitOfWork->getDocumentIdentifier($document); $dbRef = array($this->cmd . 'ref' => $class->getCollection(), $this->cmd . 'id' => $class->getDatabaseIdentifierValue($id), $this->cmd . 'db' => $class->getDatabase()); // add a discriminator value if the referenced document is not mapped explicitely to a targetDocument if ($referenceMapping && !isset($referenceMapping['targetDocument'])) { $discriminatorField = isset($referenceMapping['discriminatorField']) ? $referenceMapping['discriminatorField'] : '_doctrine_class_name'; $discriminatorValue = isset($referenceMapping['discriminatorMap']) ? array_search($class->getName(), $referenceMapping['discriminatorMap']) : $class->getName(); $dbRef[$discriminatorField] = $discriminatorValue; } return $dbRef; }
/** * {@inheritdoc} */ public function clear() { if ($this->initialized && $this->isEmpty()) { return; } if ($this->mapping !== null && isset($this->mapping['embedded'])) { foreach ($this->coll as $element) { $this->uow->scheduleOrphanRemoval($element); } } $this->mongoData = array(); $this->coll->clear(); $this->changed(); $this->uow->scheduleCollectionDeletion($this); $this->takeSnapshot(); }
/** * Returns a DBRef array for the supplied document. * * @param mixed $document A document object * @param array $referenceMapping Mapping for the field the references the document * * @return array A DBRef array */ public function createDBRef($document, array $referenceMapping = null) { if (!is_object($document)) { throw new \InvalidArgumentException('Cannot create a DBRef, the document is not an object'); } $className = get_class($document); $class = $this->getClassMetadata($className); $id = $this->unitOfWork->getDocumentIdentifier($document); $dbRef = array($this->cmd . 'ref' => $class->getCollection(), $this->cmd . 'id' => $class->getDatabaseIdentifierValue($id), $this->cmd . 'db' => $this->getDocumentDatabase($className)->getName()); if ($class->discriminatorField) { $dbRef[$class->discriminatorField['name']] = $class->discriminatorValue; } // add a discriminator value if the referenced document is not mapped explicitely to a targetDocument if ($referenceMapping && !isset($referenceMapping['targetDocument'])) { $discriminatorField = isset($referenceMapping['discriminatorField']) ? $referenceMapping['discriminatorField'] : '_doctrine_class_name'; $discriminatorValue = isset($referenceMapping['discriminatorMap']) ? array_search($class->getName(), $referenceMapping['discriminatorMap']) : $class->getName(); $dbRef[$discriminatorField] = $discriminatorValue; } return $dbRef; }
/** * Clears the DocumentManager. All documents that are currently managed * by this DocumentManager become detached. * * @param string $documentName */ public function clear() { $this->_unitOfWork->clear(); }
/** * Determines whether a document instance is managed in this DocumentManager. * * @param object $document * @return boolean TRUE if this DocumentManager currently manages the given document, FALSE otherwise. */ public function contains($document) { return $this->unitOfWork->isScheduledForInsert($document) || $this->unitOfWork->isInIdentityMap($document) && !$this->unitOfWork->isScheduledForDelete($document); }
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; }
/** @override */ public function sort($fields) { $fields = $this->unitOfWork->getDocumentPersister($this->class->name)->prepareSort($fields); $fields = parent::sort($fields); return $this; }