簡體   English   中英

使用Cakephp 3搜索表單

[英]Search form with 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();  ?>

現在,如何顯示查詢結果?

您的查詢應該是這樣的。

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

要顯示搜索結果,請執行以下“其他”操作:1:在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']);
        }

第三:編輯您的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>

在上面的視圖中,根據您的表編輯表的實體名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM