/**
  * @param string $sql
  * @param array $args
  * @param SearchResult $result
  * @param string $orderAndLimitQuery
  * @param string $idColumn
  */
 private function executeSearchQuery($sql, $args, $result, $orderAndLimitQuery, $idColumn = 'id')
 {
     $numStatement = $this->db->prepare(str_replace('SELECT ' . $idColumn . ' FROM', 'SELECT COUNT(*) FROM', $sql));
     $numStatement->execute($args);
     $result->setNumMatching(intval($numStatement->fetchColumn()));
     $sql .= $orderAndLimitQuery;
     $searchStatement = $this->db->prepare($sql);
     $searchStatement->execute($args);
     $objects = array();
     while ($row = $searchStatement->fetch()) {
         $objects[] = $this->loadObject($row[$idColumn]);
     }
     $result->setObjects($objects);
 }
Exemple #2
0
 /**
  * @inheritDoc
  */
 public function search($object, $search, $offset = 0, $limit = 50)
 {
     if (empty($search)) {
         $object .= '?q=all';
     } else {
         $query = '';
         foreach ($search as $key => $val) {
             $query .= 'q[' . urldecode($key) . ']=' . urlencode($val) . '&';
         }
         if ($query == 'q[]=&') {
             $object .= '?q=all';
         } else {
             $object .= '?' . rtrim($query, '&');
         }
     }
     $object .= '&offset=' . $offset . '&limit=' . $limit;
     $response = $this->request('get', $object);
     if ($response->status == 200) {
         $result = new SearchResult($offset, $limit);
         $result->setQuery($response->body->query);
         $result->setNumMatching($response->body->matching);
         $result->setObjects($response->body->objects);
         return $result;
     } else {
         throw new ClientException($this->lastResponse, $response->status);
     }
 }