Форма поиска с Cakephp 3

я пытаюсь создать форму поиска в моем приложении, я хочу найти пост с одним входом и его поиск по названию и содержанию.

РЕДАКТИРОВАТЬ : Я использую CakePHP Плагин поиска
Мой контроллер

public function initialize(){
parent::initialize();
$this->loadComponent('Search.Prg', ['actions'=>'index','lookup']);
}
public function search(){
$query = $this->Posts
->find('search',['search' => $this->request->query])
->contain(['Users','Categories'])
->where(['Posts.status' => 1]);

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

Моя модель

use Search\Manager;
$this->addBehavior('Search.Search');
$this->searchManager()
->add('q', 'Search.Like', [
'before' => true,
'after' => true,
'mode' => 'or',
'comparison' => 'LIKE',
'wildcardAny' => '*',
'wildcardOne' => '?',
'field' => [$this->aliasField('name'), $this->aliasField('content')]
]);

И на мой взгляд

 <?= $this->Form->create();  ?>
<?= $this->Form->input('q');  ?>
<?= $this->Form->button('Search', ['action'=>'index']);  ?>
<?= $this->Form->end();  ?>

Теперь, как показать результаты запроса?

0

Решение

Ваш запрос должен быть примерно таким.

$query = $this->find('all')
->where(['title LIKE' => '%' . $this->request->query('q') . '%'])
->orWhere(['content LIKE' => '%' . $this->request->query('q') . '%']);
0

Другие решения

Для отображения результатов поиска выполните следующие «дополнительные» действия:
первая:
Загрузите ваш компонент в AppController

class AppController extends Controller
{
public function initialize()
{
parent::initialize();

$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Search.Prg', [
// This is default config. You can modify "actions" as needed to make
// the PRG component work only for specified methods.
'actions' => ['index', 'lookup']
]);
}

второй:
Добавьте следующий код в ваш метод поиска:

$query = $this->SystemUsers->find('search', ['search' => $this->request->query]);
$vari = $this->request->getQuery('q');
if ($vari != "") {
$posts= $this->paginate($query);

$this->set(compact('posts'));
$this->set('_serialize', ['posts']);
}

3-е: отредактируйте файл search.ctp для просмотра результата:

<table class="table" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th style="width: 3%;"></th>
<th scope="col"><?= $this->Paginator->sort('id') ?></th>
<th scope="col"><?= $this->Paginator->sort('name') ?></th>
<th scope="col"><?= $this->Paginator->sort('content') ?></th>
<th scope="col"><?= $this->Paginator->sort('created') ?></th>
<th scope="col"><?= $this->Paginator->sort('modified') ?></th>
<th scope="col" class="actions"><?= __('Actions') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($posts as $post): ?>
<tr>
<td style="width: 3%;"><input type="checkbox" value=""></td>
<td><?= $this->Number->format($post->id) ?></td>
<td><?= h($post->name) ?></td>
<td><?= h($post->content) ?></td>
<td><?= h($post->created) ?></td>
<td><?= h($post->modified) ?></td>
<td class="actions">
<?= $this->Html->link(__('View'), ['action' => 'view', $post->id]) ?>
<?= $this->Html->link(__('Edit'), ['action' => 'edit', $post->id]) ?>
<?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $post->id], ['confirm' => __('Are you sure you want to delete # {0}?', $post->id)]) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div class="paginator">
<ul class="pagination">
<?= $this->Paginator->first('<< ' . __('first')) ?>
<?= $this->Paginator->prev('< ' . __('previous')) ?>
<?= $this->Paginator->numbers() ?>
<?= $this->Paginator->next(__('next') . ' >') ?>
<?= $this->Paginator->last(__('last') . ' >>') ?>
</ul>
<p><?= $this->Paginator->counter(['format' => __('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')]) ?></p>
</div>

В представлении выше измените имя сущности таблицы в соответствии с вашей таблицей.

0