Beispiel #1
6
 /**
  * @param QueryInterface $query
  * @param ModelInterface $model
  * @return ModelListInterface
  * @throws NotSupportedFilterException
  */
 public function find(QueryInterface $query, ModelInterface $model)
 {
     $solrQuery = $this->solrClient->createSelect();
     $helper = $solrQuery->getHelper();
     foreach ($query->getFilters() as $filter) {
         if (!$filter instanceof KeyValueFilter) {
             throw new NotSupportedFilterException(sprintf('%s filter is not supported or unknown.', get_class($filter)));
         }
         $solrQuery->createFilterQuery($filter->getFieldName())->setQuery($filter->getFieldName() . ': ' . $helper->escapePhrase($filter->getValue()));
     }
     if ($query->getLimit() !== null) {
         $solrQuery->setRows($query->getLimit());
     }
     if ($query->getOffset() !== null) {
         $solrQuery->setStart($query->getOffset());
     }
     $result = $this->solrClient->select($solrQuery);
     $list = new ModelList();
     foreach ($result as $doc) {
         /** @var ModelInterface $item */
         $item = new $model();
         $item->loadData($doc->getFields());
         if ($item instanceof SavableModelInterface) {
             $item->markAsStored();
         }
         $list->addListItem($item);
     }
     return $list;
 }
 /**
  * @param QueryInterface $query
  * @param ModelInterface $model
  * @return ModelListInterface
  * @throws NotSupportedFilterException
  */
 public function find(QueryInterface $query, ModelInterface $model)
 {
     $queryArray = array();
     foreach ($query->getFilters() as $filter) {
         if (!in_array(get_class($filter), self::$supportedFilters)) {
             throw new NotSupportedFilterException(sprintf('%s filter is not supported or unknown.', get_class($filter)));
         }
         $queryArray[$filter->getFieldName()] = $filter->getValue();
     }
     $options = array();
     if ($query->getLimit() !== null) {
         $options['limit'] = $query->getLimit();
     }
     if ($query->getOffset() !== null) {
         $options['skip'] = $query->getOffset();
     }
     $list = new ModelList();
     $cursor = $this->collection->find($queryArray, $options);
     /** @var \MongoDB\Model\BSONDocument $doc */
     foreach ($cursor as $doc) {
         $docArray = (array) $doc;
         unset($docArray['_id']);
         /** @var ModelInterface $item */
         $item = new $model();
         $item->loadData($docArray);
         if ($item instanceof SavableModelInterface) {
             $item->markAsStored();
         }
         $list->addListItem($item);
     }
     return $list;
 }
Beispiel #3
0
 /**
  * @tests
  */
 public function isEmpty_false()
 {
     $list = new ModelList();
     $list->addListItem(new ModelFixture());
     $this->assertFalse($list->isEmpty());
 }
 /**
  * @param QueryInterface $query
  * @param ModelInterface $model
  * @return ModelListInterface
  * @throws NotSupportedFilterException
  */
 public function find(QueryInterface $query, ModelInterface $model)
 {
     $params = ['index' => $this->index, 'type' => $this->type, 'body' => []];
     $queryArray = [];
     foreach ($query->getFilters() as $filter) {
         if (!$filter instanceof KeyValueFilter) {
             throw new NotSupportedFilterException(sprintf('%s filter is not supported or unknown.', get_class($filter)));
         }
         if (!isset($queryArray['match'])) {
             $queryArray['match'] = [];
         }
         $queryArray['match'][$filter->getFieldName()] = $filter->getValue();
     }
     if (!empty($queryArray)) {
         $params['body'] = ['query' => $queryArray];
     }
     if ($query->getLimit()) {
         $params["size"] = $query->getLimit();
     }
     if ($query->getOffset()) {
         $params['from'] = $query->getOffset();
     }
     $results = $this->client->search($params);
     $list = new ModelList();
     foreach ($results['hits']['hits'] as $doc) {
         /** @var ModelInterface $item */
         $doc['_source']['id'] = $doc['_id'];
         $item = new $model();
         $item->loadData($doc['_source']);
         if ($item instanceof SavableModelInterface) {
             $item->markAsStored();
         }
         $list->addListItem($item);
     }
     return $list;
 }
Beispiel #5
0
 /**
  * @param QueryInterface $query
  * @param ModelInterface $model
  * @return ModelListInterface
  * @throws NotSupportedFilterException
  */
 public function find(QueryInterface $query, ModelInterface $model)
 {
     $bindData = array();
     $sql = $this->getSqlFindQuery($query, $bindData);
     $query = $this->pdo->prepare($sql);
     $query->execute($bindData);
     $list = new ModelList();
     while ($row = $query->fetch(\PDO::FETCH_ASSOC)) {
         /** @var ModelInterface $item */
         $item = new $model();
         $item->loadData($row);
         if ($item instanceof SavableModelInterface) {
             $item->markAsStored();
         }
         $list->addListItem($item);
     }
     return $list;
 }