示例#1
0
 /**
  * Generates the query to search rows.
  * 
  * @param SearchQuery $search
  * 
  * @return \SimpleCrud\Queries\Query
  */
 protected function getQuery(SearchQuery $search)
 {
     $table = $this->getTable();
     $query = $table->select();
     //Filter by id
     if (count($search->getIds())) {
         $query->byId($search->getIds());
     }
     if ($search->getPage() !== null) {
         $limit = $search->getLimit();
         $query->offset($search->getPage() * $limit - $limit)->limit($limit);
     }
     if ($this->searchFields === null) {
         $this->searchFields = [$this->getFirstField()];
     }
     $orderBy = $search->getSort();
     if (!empty($orderBy) && isset($table->getScheme()['fields'][$orderBy])) {
         $query->orderBy("`{$table->name}`.`{$orderBy}`", $search->getDirection());
     } else {
         $query->orderBy("`{$table->name}`.`id`", 'DESC');
     }
     //Filter by words
     foreach ($search->getWords() as $k => $word) {
         foreach ($this->searchFields as $field) {
             $query->where("`{$table->name}`.`{$field}` LIKE :w{$k}", [":w{$k}" => "%{$word}%"]);
         }
     }
     //Filter by relations
     $db = $table->getDatabase();
     foreach ($search->getConditions() as $name => $value) {
         $related = $db->{$name}->select()->by('id', $value)->run();
         $query->relatedWith($related);
     }
     return $query;
 }
示例#2
0
文件: File.php 项目: oscarotero/folk
 /**
  * {@inheritdoc}
  */
 public function search(SearchQuery $search)
 {
     $result = [];
     $words = $search->getWords();
     $start = strlen($this->getBasePath()) + 1;
     $length = -strlen($this->extension) - 1;
     $ids = $search->getIds();
     foreach ($this->getIterator() as $file) {
         if (!$file->isFile() || $file->getExtension() !== $this->extension) {
             continue;
         }
         $id = substr($file->getPathname(), $start, $length);
         //Filter by id
         if (!empty($ids) && !in_array($id, $ids, true)) {
             continue;
         }
         //Filter by word
         foreach ($words as $word) {
             if (strpos($id, $word) === false) {
                 continue;
             }
         }
         $result[$id] = $this->parse(file_get_contents($file->getPathname()));
     }
     $sort = $search->getSort();
     if ($sort !== null) {
         uasort($result, function ($a, $b) use($sort) {
             if ($a[$sort] === $b[$sort]) {
                 return 0;
             }
             return $a[$sort] < $b[$sort] ? -1 : 1;
         });
         if ($search->getDirection() === 'DESC') {
             $result = array_reverse($result, true);
         }
     }
     if ($search->getPage() !== null) {
         $limit = $search->getLimit();
         $offset = $search->getPage() * $limit - $limit;
         $result = array_slice($result, $offset, $limit, true);
     }
     return $result;
 }