/** * @param PersistentCollection $collection * * @return CursorInterface */ public function createReferenceManyWithRepositoryMethodCursor(PersistentCollection $collection) { $hints = $collection->getHints(); $mapping = $collection->getMapping(); $cursor = $this->dm->getRepository($mapping['targetDocument'])->{$mapping}['repositoryMethod']($collection->getOwner()); if (!$cursor instanceof CursorInterface) { throw new \BadMethodCallException("Expected repository method {$mapping['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; }
/** * 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); }
/** * 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; }
/** * 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); } }
/** * 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; } } }