Example #1
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}'");
     }
     // Return results depending on hydration mode
     if ($this->hydrationMode == self::HYDRATE_BYPASS) {
         return $resultSet;
     } elseif ($this->hydrationMode == self::HYDRATE_INTERNAL) {
         return $this->sm->getUnitOfWork()->hydrateCollection($classes, $resultSet);
     }
     // 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);
 }
Example #2
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();
     }
 }
Example #3
0
 /**
  * Commit deleted entities to the database
  */
 private function commitRemoved()
 {
     $documents = $this->sortObjects($this->scheduledForDelete, false);
     $client = $this->sm->getClient();
     foreach ($documents as $entityName => $documents) {
         $classMetadata = $this->sm->getClassMetadata($entityName);
         $this->updatedIndexes[] = $classMetadata->index;
         $client->removeDocuments($classMetadata, $documents);
     }
 }
Example #4
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'));
 }