我是CakePHP的新手,所以这可能是初学者的问题。
我正在尝试通过Friendsofcake安装CakePHP的搜索插件(https://github.com/FriendsOfCake/search)
使用上述链接中的自述文件,我无法通过字段电子邮件(varchar)和 RG (整数)搜索用户
但是,当我尝试访问../Users时,系统会在本文末尾显示错误。 错误似乎就好像方法 findParams 无效,即使我的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
⟩蛋糕\ ORM \表 - > __调用APP / Controller \ UsersController.php,第42行⟩ App \ Controller \ UsersController->索引[内部功能]⟩ call_user_func_array ROOT \供应商\ friendsofcake \ CRUD的\ src \控制器\ ControllerTrait.php, 第51行⟩App\ Controller \ AppController-> invokeAction CORE \ src \ Routing \ Dispatcher.php,第114行⟩ Cake \ Routing \ Dispatcher-> _invoke CORE \ src \ Routing \ Dispatcher.php,line 87⟩Cake\ Routing \ Dispatcher-> dispatch ROOT \ webroot \ index.php,第36行
答案 0 :(得分:0)
您可能打算在UsersController中将$this->set('articles', $this->paginate($query));
更改为$this->set('users', $this->paginate($query));
。同时将UsersTable中的class UsersTable extends Cake\ORM\Table
更改为class UsersTable extends Table
- 可能会有更多错误,您可以使用插件的标准示例检查代码。