繁体   English   中英

无法通过Friendsofcake安装CakePHP的搜索插件

[英]Unable to install Search plugin for CakePHP by Friendsofcake

我是CakePHP的新手,所以这可能是一个初学者的问题。

我正在尝试由Friendsofcake( https://github.com/FriendsOfCake/search )安装CakePHP的搜索插件

使用上面链接中的自述文件,我可以按电子邮件 (varchar)和RG (Integer)字段搜索我的用户

但是,当我尝试访问../Users时,系统会在本文结尾处显示错误。 错误似乎是方法findParams无效,即使Search插件已加载到我的bootstrap.php中也是如此。

我有以下UsersTable.php和UsersController.php:

UsersTable:

use Search\Manager;


class UsersTable extends Cake\ORM\Table {

    public function initialize(array $config)
    {
        parent::initialize();
        // Add the behaviour to your table
        $this->addBehavior('Search.Search');

    }
    // Configure how you want the search plugin to work with this table class
    public function searchConfiguration()
    {
        $search = new Manager($this);
        $search
            ->value('email', [
                'field' => $this->aliasField('email'),
            ])
            // Here we will alias the 'q' query param to search the `Users.email`
            // field and the `Users.rg` field, using a LIKE match, with `%`
            // both before and after.
            ->like('q', [
                'before' => true,
                'after' => true,
                'field' => [$this->aliasField('email'), $this->aliasField('rg')]
            ])
            ->callback('foo', [
                'callback' => function ($query, $args, $manager) {
                    // Modify $query as required
                }
            ]);

        return $search;
    }

}

UsersController:

namespace App\Controller;

class UsersController extends AppController
{
    public function initialize()
    {
        parent::initialize();
        $this->loadComponent('Search.Prg', [
            'actions' => ['index']
        ]);
    } 

    public function login() {
    if ($this->request->is('post')) {
            $user = $this->Auth->identify();
            if ($user) {
                    $this->Auth->setUser($user);
                    return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Flash->error('Your username or password is incorrect.');
    }
    }
    public function logout() {
            $this->Flash->success('You are now logged out.');
            return $this->redirect($this->Auth->logout());
    }

    public function index()
    {
        $query = $this->Users
            // Use the plugins 'search' custom finder and pass in the
            // processed query params
            ->find('search', $this->Users->filterParams($this->request->query))
            // You can add extra things to the query if you need to
            ->contain(['rg'])
            ->where(['email IS NOT' => null]);

        $this->set('articles', $this->paginate($query));
    }
}

现在我得到了错误:

未知的方法“ filterParams” BadMethodCallException

⟩Cake \\ ORM \\ Table-> __ call APP / Controller \\ UsersController.php,第42行⟩App \\ Controller \\ UsersController-> index [内部函数]⟩call_user_func_array ROOT \\ vendor \\ friendsofcake \\ crud \\ src \\ Controller \\ Controller \\ ControllerTrait.php,第51行⟩App \\ Controller \\ AppController-> invokeAction CORE \\ src \\ Routing \\ Dispatcher.php,第114行⟩Cake \\ Routing \\ Dispatcher-> _ invoke CORE \\ src \\ Routing \\ Dispatcher.php,第87行⟩Cake \\ Routing \\ Dispatcher ->调度ROOT \\ webroot \\ index.php,第36行

您可能打算更改$this->set('articles', $this->paginate($query)); $this->set('users', $this->paginate($query)); 在您的UsersController中。 同样,将class UsersTable extends Cake\\ORM\\Table为UsersTable class UsersTable extends Table可能会有更多此类错误,您需要使用插件的标准示例来检查代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM