atk14/search-field

ATK14应用的搜索字段

dev-master 2018-12-13 12:31 UTC

This package is auto-updated.

Last update: 2024-09-14 03:15:46 UTC


README

SearchField 是在 ATK14 应用程序中输入搜索术语的表单字段。

基本来说,它渲染 <input type="search"> 元素。

安装

只需使用 Composer

cd path/to/your/atk14/project/
composer require atk14/search-field

可选地,您可以将 SearchField 文件链接到您的项目中

ln -s ../../vendor/atk14/search-field/src/app/fields/search_field.php app/fields/search_field.php
ln -s ../../vendor/atk14/search-field/src/app/widgets/search_widget.php app/widgets/search_widget.php

在 ATK14 应用程序中的使用

在表单中

<?php
// file: app/forms/articles/index_form.php
class IndexForm extends ApplicationForm {

  function set_up(){
    $this->add_field("q", new SearchField([
      "label" => "Search",
      "max_length" => 50, // default is 200
      // by default the field is not required
    ]));

  }
}

在控制器中

<?php
// file: app/controllers/articles_controller.php
class ArticlesController extends ApplicationController {
  
  function index(){
    $this->page_title = "Listing Articles";

    ($d = $this->form->validate($this->params)) || ($d = $this->form->get_initial());

    $conditions = $bind_ar = [];
    $conditions[] = "published_at<NOW()";

    if($d["q"]){
      $ft_cond = FullTextSearchQueryLike::GetQuery("UPPER(title||' '||body)",mb_strtoupper($d["q"]),$bind_ar);
      if($ft_cond){
        $conditions[] = $ft_cond;
      }
    }

    $this->tpl_data["finder"] = Article::Finder([
      "conditions" => $conditions,
      "bind_ar" => $bind_ar,
      "limit" => 20,
      "offset" => $this->params->getInt("offset"),
      "order_by" => "published_at DESC, id DESC",
    ]);
  }
}

在上面的示例中使用了 FullTextSearchQueryLike 类。它使用 SQL 操作符 LIKE 提供类似于全文搜索的感觉。您可以使用 Composer 安装它

composer require yarri/full-text-search-query-like

许可

SearchField 是免费软件,根据 MIT 许可证分发 条款