Exemple #1
0
 /**
  * Delete the given document's indexes.
  *
  * @param string $documentName
  * @throws \InvalidArgumentException
  */
 public function deleteDocumentIndexes($documentName)
 {
     $class = $this->dm->getClassMetadata($documentName);
     if ($class->isMappedSuperclass || $class->isEmbeddedDocument) {
         throw new \InvalidArgumentException('Cannot delete document indexes for mapped super classes or embedded documents.');
     }
     $this->dm->getDocumentCollection($documentName)->deleteIndexes();
 }
 /**
  * Executes a query updating the given document.
  *
  * @param object $document
  * @param array $newObj
  * @param array $options
  */
 private function executeQuery($document, array $newObj, array $options)
 {
     $className = get_class($document);
     $class = $this->dm->getClassMetadata($className);
     $id = $class->getDatabaseIdentifierValue($this->uow->getDocumentIdentifier($document));
     $query = array('_id' => $id);
     if ($class->isVersioned) {
         $query[$class->versionField] = $class->reflFields[$class->versionField]->getValue($document);
     }
     $collection = $this->dm->getDocumentCollection($className);
     $result = $collection->update($query, $newObj, $options);
     if ($class->isVersioned && !$result['n']) {
         throw LockException::lockFailed($document);
     }
 }
 /** @inheritDoc */
 public function generate(DocumentManager $dm, $document)
 {
     $className = get_class($document);
     $db = $dm->getDocumentDatabase($className);
     $coll = $this->collection ?: 'doctrine_increment_ids';
     $key = $this->key ?: $dm->getDocumentCollection($className)->getName();
     $query = array('_id' => $key);
     $newObj = array('$inc' => array('current_id' => 1));
     $command = array();
     $command['findandmodify'] = $coll;
     $command['query'] = $query;
     $command['update'] = $newObj;
     $command['upsert'] = true;
     $command['new'] = true;
     $result = $db->command($command);
     return $result['value']['current_id'];
 }
Exemple #4
0
 /**
  * @param string[]|string $documentName an array of document names or just one.
  */
 private function setDocumentName($documentName)
 {
     if (is_array($documentName)) {
         $documentNames = $documentName;
         $documentName = $documentNames[0];
         $metadata = $this->dm->getClassMetadata($documentName);
         $discriminatorField = $metadata->discriminatorField;
         $discriminatorValues = $this->getDiscriminatorValues($documentNames);
         // If a defaultDiscriminatorValue is set and it is among the discriminators being queries, add NULL to the list
         if ($metadata->defaultDiscriminatorValue && array_search($metadata->defaultDiscriminatorValue, $discriminatorValues) !== false) {
             $discriminatorValues[] = null;
         }
         $this->field($discriminatorField)->in($discriminatorValues);
     }
     if ($documentName !== null) {
         $this->collection = $this->dm->getDocumentCollection($documentName);
         $this->class = $this->dm->getClassMetadata($documentName);
         // Expr also needs to know
         $this->expr->setClassMetadata($this->class);
     }
 }
 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);
             }
         }
     }
 }