Issue
I am new to cakePHP, so this may be a beginner's question.
I am trying to install the Search plugin for CakePHP by Friendsofcake (https://github.com/FriendsOfCake/search)
Using the Readme in the above link, I qant to search my Users by the fields email (varchar) and RG (Integer).
But, as I try to visit ../Users the system presents the error by the end of this text. The error seems to be as if the method findParams is not valid, even if the Search plugin is loaded in my bootstrap.php.
I have the following UsersTable.php and 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));
}
}
And now i am getting the error:
Unknown method "filterParams" BadMethodCallException
⟩ Cake\ORM\Table->__call APP/Controller\UsersController.php, line 42 ⟩ App\Controller\UsersController->index [internal function] ⟩ call_user_func_array ROOT\vendor\friendsofcake\crud\src\Controller\ControllerTrait.php, line 51 ⟩ App\Controller\AppController->invokeAction CORE\src\Routing\Dispatcher.php, line 114 ⟩ Cake\Routing\Dispatcher->_invoke CORE\src\Routing\Dispatcher.php, line 87 ⟩ Cake\Routing\Dispatcher->dispatch ROOT\webroot\index.php, line 36
Solution
you probably meant to change $this->set('articles', $this->paginate($query));
to $this->set('users', $this->paginate($query));
in your UsersController. Also change class UsersTable extends Cake\ORM\Table
in UsersTable to class UsersTable extends Table
- there may be more errors like this for you to check in your code with the standard examples for the plugin.
Answered By - Mat K. Witts
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.