CakePHP 検索機能(friendsofcake/search)

インストール

composer require friendsofcake/search

設定

cd bin
cake plugin load Search

※~/config/bootstrap.php
Plugin::load('Search');

完全一致検索

ビュー

<form method="post" accept-charset="utf-8" action="~">
<div><?= $this->Form->text('name') ?></div>
<input type='submit' value='検索' />
<?= $this->Form->end() ?>

モデル

※~Table
public function initialize(array $config)
{
  parent::initialize($config);

  $this->addBehavior('Search.Search');
  $this->searchManager()
    ->value('name');
}

コントローラ

※~Controller
public function initialize()
{
  parent::initialize();

  $this->loadComponent('Search.Prg', [
    'actions' => ['(アクション名)']
  ]);
}

public function (アクション名)(){
  $query = $this->(モデル名)
    ->find('search', ['search'=>$this->request->query]);
  $this->set('(変数名)', $this->paginate($query));
}

あいまい検索

ビュー

<form method="post" accept-charset="utf-8" action="~">
<div><?= $this->Form->text('name') ?></div>
<input type='submit' value='検索' />
<?= $this->Form->end() ?>

モデル

※~Table
public function initialize(array $config)
{
  parent::initialize($config);

  $this->addBehavior('Search.Search');
  $this->searchManager()
    ->like('name',[
      'before' => true,
      'after' => true
    ]);
}

複数カラム検索

ビュー

<form method="post" accept-charset="utf-8" action="~">
<div><?= $this->Form->text('name1') ?></div>
<div><?= $this->Form->text('name2') ?></div>
<div><?= $this->Form->text('name3') ?></div>
<input type='submit' value='検索' />
<?= $this->Form->end() ?>

モデル

※~Table
public function initialize(array $config)
{
  parent::initialize($config);

  $this->addBehavior('Search.Search');
  $this->searchManager()
    ->add('name1',
      'Search.Like', [
      'before' => true,
    'after' => true,
    'mode' => 'or',
    'comparison' => 'LIKE',
    'wildcardAny' => '*',
    'wildcardOne' => '?',
    'field' => ['name1', 'name2', 'name3']
 ])
}

日付範囲検索

ビュー

<form method="post" accept-charset="utf-8" action="~">
<?= $this->Form->control('start_created', [
 'type'=>'text',
 'label'=>'開始',
 'id'=>'date_start']); ?>
<?= $this->Form->control('end_created', [
 'type'=>'text',
 'label'=>'終了',
 'id'=>'date_end']); ?>

jQuery処理
<?= $this->Html->scriptStart(['block' => true]) ?>
 $( function() {
  $( "#date_start" ).datepicker({
   dateFormat: 'yy-mm-dd'
  });
  $( "#date_end" ).datepicker({
   dateFormat: 'yy-mm-dd'
  });
 } );
<?= $this->Html->scriptEnd() ?>

<input type='submit' value='検索' />
<?= $this->Form->end() ?>

モデル

※~Table
public function initialize(array $config)
{
  parent::initialize($config);

  $this->addBehavior('Search.Search');
  $this->searchManager()
    ->callback('start_created', [
   'callback' => function ($query, $args, $filter) {
    if(empty($args['end_created'])) {
     return;
    }
    return $query->where(function ($exp) use ($args) {
     時間の最小/最大を付与
     $args['start_created'] .= ' 00:00:00';
     $args['end_created'] .= ' 23:59:59';
     return $exp->between(
       'products.created',
       $args['start_created'],
       $args['end_created']
      );
     });
    }
   ])

->callback('end_created', [
   'callback' => function ($query, $args, $filter) {}
 ])
}

別モデル検索

モデル

※~Table
public function initialize(array $config)
{
  parent::initialize($config);
  $this->addBehavior('Search.Search');
  $this->searchManager()

    他の検索と組み合わせ可
    ->like('col',[
      'before' => true,
      'after' => true,
      'field' => $this->aliasField('others.col')
    ])
}

コントローラ

※~Controller
public function initialize()
{
  parent::initialize();
  $this->loadComponent('Search.Prg', [
    'actions' => ['(アクション名)']
  ]);

}

public function (アクション名)(){
  $query = $this->(モデル名)
    ->find('search', ['search'=>$this->request->query]);
    ->contain(['others'])
  $this->set('(変数名)', $this->paginate($query));
}

コメントを残す

メールアドレスが公開されることはありません。