【问题标题】:Cakephp - Index searchCakephp - 索引搜索
【发布时间】:2018-09-12 07:37:43
【问题描述】:

我有一个关于如何搜索的问题,例如,在 INDEX 视图中按姓名或姓氏进行搜索。 是否有任何插件或易于使用的组件不会过多地加载应用程序?

目前,我在 index.ctp 中有一个带有一个输入字段的小表单,我在其中输入要搜索的内容,并且在控制器中有一个 if ($ this-> request-> is ('post' )) 之后 $this->Model->find 添加条件 WHERE。但我承认这不是一个好方法,它还会重新加载页面。

这是控制器:

public function index()
{
    if ($this->request->is('post')) {
        $s = '%'.$this->request->getData('Search').'%';
        $students = $this->Students->find('all')->where(['OR' => ['name LIKE' => $s, 'lastname LIKE' => $s]]);
        $this->set('students', $this->paginate($students));
    } else {
        $students = $this->paginate($this->Students);
        $this->set(compact('students'));
    }
}

这是 index.ctp:

<div class="input-group mb-3">
    <?= $this->Form->button(__('<i class="fas fa-search"></i>'), ['escape' => false, 'class' => 'btn btn-primary']) ?>
    <?= $this->Form->input('Search', ['type' => 'text', 'class' => 'form-control', 'label' => false]); ?>
    <?= $this->Form->end() ?>
</div>

【问题讨论】:

    标签: php cakephp search plugins cakephp-3.x


    【解决方案1】:

    据我所知,您要么寻求更好的搜索处理解决方案和/或不重新加载页面以显示搜索结果的方式。 如果这不是您所追求的,请澄清您的问题,以更好地概述您希望解决方案的外观。

    您应该将搜索与索引功能分开,因为这只是为了显示该页面上的内容。添加像你这样的条件最终会导致你有一个非常长的索引函数来根据请求的差异运行你的整个应用程序,这不是很好。

    将搜索拆分为一个单独的函数,然后创建一个新页面供您显示结果。

    public function index()
    {
        $students = $this->paginate($this->Students);
        $this->set(compact('students'));
    }
    
    public function search()
    {
        $s = '%' . $this->request->getData('Search') . '%';
        $students = $this->Students->find('all')->where(['OR' => ['name LIKE' => $s, 'lastname LIKE' => $s]]);
        $this->set('students', $this->paginate($students));
    }
    

    CakePHP 文档的教程 - CMS Tutorial - Creating the Articles Controller 对此进行了很好的描述,并且可以与您的应用程序相关,因为您的索引包含将搜索条件传递给您的应用程序的表单,然后搜索功能/页面将获取结果并为您显示。

    不要忘记更改您的表单以将其指向/search 页面。 - Setting a URL for the Form

    您需要在 src/Template/ExampleController 文件夹中创建一个 search.ctp 文件。将您的 html 代码放在那里以在表格中显示结果,或者您想要显示搜索结果。

    最后,您需要在您的 routes.php 中添加一个路由,以允许在您的应用程序中使用 /search 路径。

    $routes->connect('/search', ['controller' => 'Example', 'action' => 'search']);
    

    现在,如果您不想在使用表单时重新加载页面,那么您必须使用 Ajax 和 JS/JQuery 向您的搜索方法发出请求,并在页面上动态显示搜索功能的结果。在 stackoverflow 和 web 上已经有很多很好的例子,用于使用 ajax 和 jquery 从搜索中构建表格,所以我不会费心在这里发布它们。

    如果您需要插件/库解决方案,请查看本教程,了解如何使用 DataTables 在表格中显示搜索结果。 - Search Using Datatables

    【讨论】:

      【解决方案2】:

      安装插件https://github.com/FriendsOfCake/search

      然后在搜索配置中

      $this->searchManager()
           // Here we will alias the 'q' query param to search the `Students.name`
           // field and the `Students.lastname` field, using a LIKE match, with `%`
                  // both before and after.
                  ->add('q', 'Search.Like', [
                      'before' => true,
                      'after' => true,
                      'fieldMode' => 'OR',
                      'comparison' => 'LIKE',
                      'wildcardAny' => '*',
                      'wildcardOne' => '?',
                      'field' => ['name', 'lastname']
                  ]);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-01
        • 2017-12-22
        • 1970-01-01
        相关资源
        最近更新 更多