예제 #1
0
 /**
  * Commit deleted entities to the database
  *
  * @param mixed $entity
  *
  * @throws \Exception
  */
 private function commitRemoved($entity = null)
 {
     $scheduledForDelete = $this->scheduledForDelete;
     if ($entity) {
         // if entity is presented, we need for first make sure if the entity
         // is in the scheduled for delete
         if ($this->isInScheduledForDelete($entity)) {
             $scheduledForDelete = array($entity);
         } else {
             return;
         }
     }
     $sortedDocuments = $this->sortObjects($scheduledForDelete);
     $client = $this->sm->getClient();
     foreach ($sortedDocuments as $entityName => $documents) {
         $classMetadata = $this->sm->getClassMetadata($entityName);
         foreach ($documents as $document) {
             $index = $classMetadata->getIndexForWrite($document);
             $this->updatedIndexes[$index] = $index;
         }
         $client->removeDocuments($classMetadata, $documents);
     }
     if ($entity) {
         unset($this->scheduledForDelete[spl_object_hash($entity)]);
     } else {
         $this->scheduledForDelete = array();
     }
 }
예제 #2
0
 /**
  * Execute search and hydrate results if required.
  *
  * @param integer $hydrationMode
  * @throws DoctrineSearchException
  * @return mixed
  */
 public function getResult($hydrationMode = null)
 {
     if ($hydrationMode) {
         $this->hydrationMode = $hydrationMode;
     }
     $classes = array();
     foreach ($this->entityClasses as $entityClass) {
         $classes[] = $this->sm->getClassMetadata($entityClass);
     }
     $resultSet = $this->getSearchManager()->getClient()->search($this->query, $classes);
     // TODO: abstraction of support for different result sets
     if ($resultSet instanceof Elastica\ResultSet) {
         $this->count = $resultSet->getTotalHits();
         $this->facets = $resultSet->getFacets();
         $results = $resultSet->getResults();
     } else {
         $resultClass = get_class($resultSet);
         throw new DoctrineSearchException("Unexpected result set class '{$resultClass}'");
     }
     // If having the hydration query set, hydrate using the hydrate query
     if ($this->getHydrationQuery()) {
         // Document ids are used to lookup dbms results
         $fn = function ($result) {
             return $result->getId();
         };
         $ids = array_map($fn, $results);
         return $this->getHydrationQuery()->setParameter($this->hydrationParameter, $ids ?: null)->useResultCache($this->useResultCache, $this->cacheLifetime)->getResult($this->hydrationMode);
     }
     // Return results depending on hydration mode
     switch ($this->hydrationMode) {
         case self::HYDRATE_BYPASS:
             return $resultSet;
         case self::HYDRATE_AGGREGATION:
             return $resultSet->getAggregations();
     }
     return $this->sm->getUnitOfWork()->hydrateCollection($classes, $resultSet);
 }
예제 #3
0
 public function testGetClassMetadata()
 {
     $classMetadata = new ClassMetadata(BlogPost::CLASSNAME);
     $this->metadataFactory->expects($this->once())->method('getMetadataFor')->with('Some\\Class')->will($this->returnValue($classMetadata));
     $this->assertEquals($classMetadata, $this->sm->getClassMetadata('Some\\Class'));
 }