cakephp3 FriendsOfCake/search 使い方
■composerを使ってプラグインをインストール
※同階層にcomposer.pharファイルが必要
——————
composer.pharファイルが無い場合は、下記ページの「Command-line installation」に記載してあるPHPのコマンドを実行。
https://getcomposer.org/download/
php -r ・・・ ・・・
——————
/
└─var
└─www
└─html
└─hoge.com
└─app ←A.ここにcomposerをインストールして、composer.pharファイル作成
├─bin
├─config
├─logs
├─plugins
├─src ←C.Application.php
├─tests
├─tmp
├─vendor ←B.ここにプラグイン(friendsofcake/search)がインストールされる
└─webroot
php composer.phar require friendsofcake/search
「B.のvendor」配下にfriendsofcake/searchがインストールされる
■プラグインを読み込む設定
1 or 2
1.コマンド
cd bin/ php cake.php plugin load Search
C.のApplication.phpに下記内容が追加される
public function bootstrap()
{
$this->addPlugin('Search'); ←これ
2.1.を直に書く
下記、Bake All したファイルを利用。
■Table class
public function initialize(array $config)
{
parent::initialize($config);
/* ここから追加 */
$this->addBehavior('Search.Search');
$this->searchManager()
// 完全一致 (任意のフィールド(カラム)名)
->value('tel')
// あいまい検索 (任意のフィールド(カラム)名)
->like('name', [
'before' => true,
'after' => true
])
// あいまい検索 (複数フィールド(カラム))
->add('q', 'Search.Like', [
'before' => true,
'after' => true,
'mode' => 'or',
'comparison' => 'LIKE',
'wildcardAny' => '*',
'wildcardOne' => '?',
// 任意のフィールド(カラム)名
'field' => ['name', 'address']
]);
/* ここまで追加 */
// ※ bakeした場合、下記は記述済
$this->table('テーブル名(大文字・・・s)');
$this->displayField('name');
$this->primaryKey('id');
}
■Controller class
// 任意(ページング設定)
public $paginate = [
'limit' => 100,
// 'maxLimit' => 200, // defalutで最高100件なのでそれ以上の時指定
'order' => [
'テーブル名(大文字・・・s).id' => 'asc'
]
];
public function initialize()
{
parent::initialize();
$this->loadComponent('Search.Prg', [
'actions' => ['index']
]);
}
public function index()
{
$query = $this->テーブル名(大文字・・・s)->find('search', ['search' => $this->request->query]);
$this->set('datas', $this->paginate($query));
}
■View
<?php
// debug($datas);
echo $this->Form->create();
echo $this->Form->input('tel');
echo $this->Form->input('name');
echo $this->Form->input('q');
echo $this->Form->button('Filter', ['type' => 'submit']);
echo $this->Html->link('Reset', ['action' => 'index']);
echo $this->Form->end();
?>