cakephp3で顧客テーブルをfriendsofcake/Searchで検索する部分を開発しています。
このテーブルで、住所1、2を文字列結合して検索したいです。
最終的に発行したいSQLは下記の様になるかと思います。
SQL
1select * from customers where concat(address_1, address_2) LIKE '%words(検索文字列)%'
cakephp2.xでは、仮想フィールド(virtualfield) を使い
「full_address」等の名前でconcatしたフィールドを設定しておけば
検索クエリでも、そのまま使えたらしいのですが、
cakephp3.xからは、下記理由のためできなくなったようです。
http://palmgate.co.jp/masato_kato/article.html&id=164
他画面を「firiendsofcake/searchプラグイン」を使って
開発を進めてきたため、できればプラグインを使ったまま対応したいです。
下記の記事を見つけましたが、cakephp3そのものの
経験が浅く、思うようなクエリを発行してくれません
http://stackoverflow.com/questions/37333873/concat-fields-in-search-plugin-cakephp-3
アドバイスをお願いできないでしょうか。
よろしくお願いします。
↓ C:\xampp\htdocs\cake3\src\Template\Customers\index.ctp
php
1echo $this->Form->create(); 2echo $this->Form->input('id'); //idを入力 3echo $this->Form->input('words'); //住所を入力 4echo $this->Form->button("送信", ['type' => 'submit']); 5echo $this->Form->end(); 6debug($customers);
↓ C:\xampp\htdocs\cake3\src\Model\Table\CustomersTable.php
php
1use Search\Manager; //Searchプラグイン用 2 3class CustomersTable extends Table 4{ 5 public function initialize(array $config) 6 { 7 parent::initialize($config); 8 9 /* Searchプラグイン用 */ 10 $this->addBehavior('Search.Search'); 11 $this->searchManager() 12 ->value('id') 13 ->add('words', 'Search.Like', [ 14 'before' => true, 15 'after' => true, 16 'fieldMode' => 'OR', 17 'comparison' => 'LIKE', 18 'wildcardAny' => '*', 19 'wildcardOne' => '?', 20 21 //試したこと1 仮想フィールドを指定・・・ダメ 22 // 'field' => 'full_address' 23 24 //試したこと2 concatを直接書く・・・ダメ 25 // こんなSQLになる→ WHERE Customers.concat(address_1, address_2) like '%words%' 26 // 'field' => 'concat(address_1, address_2)' 27 // ]); 28 29//試したこと3 下記を見て、コールバックを設定してみる 30//http://stackoverflow.com/questions/37333873/concat-fields-in-search-plugin-cakephp-3 31 ->add('word', 'Search.Callback', [ 32 'callback' => function ($query, $args, $manager) { 33 return $query->select([ 34 'full_address' => $query->func()->concat([ 35 'address_1' => 'identifier', 36 'address_2' => 'identifier' 37 ]) 38 ]) 39 ->having(['full_address LIKE' => '%' . $args['words'] . '%']); 40 } 41 ]); 42: 43以下略
仮想フィールドは上手くいきませんでしたが、こんなかんじで書きました。
↓C:\xampp\htdocs\cake3\src\Model\Entity\Customer.php
PHP
1class Customer extends Entity 2{ 3 protected $_virtual = [ 4 'full_address', 5 ]; 6 7 protected function _getFullAddress() 8 { 9 return $this->_properties['address_1']. 10 $this->_properties['address_2']; 11 } 12 : 13以下略
↓C:\xampp\htdocs\cake3\src\src\Controller\CustomersController.php
PHP
1class CustomersController extends AppController 2{ 3 public function initialize() 4 { 5 parent::initialize(); 6 //サーチプラグインの読み込み 7 $this->loadComponent('Search.Prg', [ 8 'actions' => ['index'] 9 ]); 10 } 11 12 public function index() 13 { 14 $selected = $this->Customers 15 ->find('search', ['search' => $this->request->query]) 16 $this->set('customers', $this->paginate($selected)); 17 : 18以下略
■参考にしたもの
http://stackoverflow.com/questions/37333873/concat-fields-in-search-plugin-cakephp-3
http://www.naidim.org/cakephp-3-tutorial-11-virtual-fields
回答2件
あなたの回答
tips
プレビュー