CakePHPã®search pluginã®ãµã³ãã«ã³ã¼ãã¨è¨äºãã¡ãã£ã¨ä¿®æ£ãContainableã¯å¿ è¦ã§ãã
CakeDCのsearch pluginの記事が少ないので1個置いときますね。CakePHP Advent Calendar 2010 8日目 - kanonjiの日記ã®ã³ã¼ããã¡ãã£ã¨ã ãç´ãã¾ãããã¿ã°æ¤ç´¢ãªã©ãå®è£ ããHABTMã®æ¤ç´¢ã§ããã©ã°ã¤ã³ã®Readme.mdã«ã¯ãã£ãContainableããã¤ãã¢ã使ããªãã£ããã§ããããã£ã±ãå¿ è¦ã§ãããç¡ãã¦ãåããã©ãä½è¨ãªJOINãçºçãã¾ãã
ä¿®æ£ç¹
$ git diff --no-prefix HEAD~ HEAD models/entry.php diff --git models/entry.php models/entry.php index 70c7aef..1b54b43 100644 --- models/entry.php +++ models/entry.php @@ -81,19 +81,23 @@ class Entry extends AppModel { } public function searchByGroupps($data = array()){ + $this->User->GrouppsUser->Behaviors->attach('Containable', array('autoFields' => false)); $this->User->GrouppsUser->Behaviors->attach('Search.Searchable'); $query = $this->User->GrouppsUser->getQuery('all', array( 'conditions' => array('groupp_id' => explode('|', $data['groupp_id'])), 'fields' => array('user_id'), + 'contain' => $this->User->Groupp->alias, )); return $query; } public function searchByTags($data = array()){ + $this->EntriesTag->Behaviors->attach('Containable', array('autoFields' => false)); $this->EntriesTag->Behaviors->attach('Search.Searchable'); $query = $this->EntriesTag->getQuery('all', array( 'conditions' => array('tag_id' => explode('|', $data['tag_id'])), 'fields' => array('entry_id'), + 'contain' => $this->Tag->alias, )); return $query; }https://github.com/kanonji/CakePHP-Search-plugin-sample/commit/38ee2a20dd374bd7bb72c8a3e814fed8c0451590
HABTMã®æ¤ç´¢ã«ã¯INæ¼ç®åã使ã£ããµãã¯ã¨ãªã¼ã使ããã¾ãã[https://github.com/CakeDC/search/blob/master/models/behaviors/searchable.php#L106:title=getQuery()]
ã§ãµãã¯ã¨ãªã¼ã®SQLãçæãããã®ã§ãæ¤ç´¢å
¨ä½ã§ã®Containableã¨ã¯å¥ã«ãContainableã®ã¢ã¿ããã¨contain
ã®è¨å®ãå¿
è¦ã¿ããã§ãã
array('autoFields' => false)ã«ã¤ãã¦
ãªããarray('autoFields' => false)
ã¯auto-add needed fields to fetch requested bindings
ã¨ãããã®ããããããåããã¾ãããtrue
ã ã¨ä½ãå¿
è¦ã¨ããããã£ã¼ã«ããåæã«è¿½å ããããã«ãªãããããããã©ã«ããtrue
ãªã®ã§æ示çã«false
ã«ãã¦ããããã§ããå®éã«true
ã®ã¾ã¾ã ã¨ã'fields' => array('user_id')
ã¨æå®ãã¦ããã®ã«Groupp.id
ã追å ããã¾ãããINæ¼ç®åã«æ¸¡ãã®ã§2ã¤ä»¥ä¸ã®ãã£ã¼ã«ããåå¾ããã¨SQLã¨ã©ã¼*1ã¨ãªã£ã¦ãã¾ãã¾ãã
SQLã§ã®éã
--- before.sql 2011-02-15 13:15:14.000000000 +0900 +++ after.sql 2011-02-15 13:15:26.000000000 +0900 @@ -27,9 +27,6 @@ FROM groupps_users AS GrouppsUser LEFT JOIN - users AS USER ON - (`GrouppsUser`.`user_id` = `User`.`id`) - LEFT JOIN groupps AS Groupp ON (`GrouppsUser`.`groupp_id` = `Groupp`.`id`) WHERE
SQLã§è¨ãã¨ãããªéãã§ããJOINããå¿ è¦ã®ãªãusersãã¼ãã«ãJOINãã¡ãã£ã¦ã¾ãããããã¯ããµã³ãã«ã¢ããªã§è¨ãã¨ããã®ã°ã«ã¼ãã§ã®æ¤ç´¢ã§ãããã¿ã°æ¤ç´¢ã®ã»ããåããã°entriesãã¼ãã«ãä½è¨ã«JOINãã¡ãã£ã¦ãã¾ããã
ã¨ããã§
ãã©ã°ã¤ã³ã®Readme.mdã ã¨ããã®ãµãã¯ã¨ãªã¼ãçæããã¡ã½ããã£ã¦findByTags($data = array())
ã¨ãã«ãªã£ã¦ããã§ããããhttp://book.cakephp.org/ja/view/1026/findByã¨è¢«ã£ã¦ãä¸ã«ãã¡ã½ããå
ã§$data['tags']
ã¨æ±ºãæã¡ã ããã³ã³ããã¼ã©ã¼ããå¼ã¹ãä½ããããªãããå¾®å¦ã ãªãã¨æã£ã¦searchByTags()
ã«ãã¦ã¿ã¾ãããã§ãããµãã¯ã¨ãªã¼ã®SQLãè²°ããã ãã®ã¡ã½ããã ããããã£ã¨å¥ã®ååã®æ¹ãè¯ãã£ããªã¨æã£ããã
ç°å¢
Mac | Mac OS X 10.5.8ï¼Leopardï¼ |
MAMP | 1.7.2 |
CakePHP | 1.3.6 |
php | 5.2.6 |
CakeDC Search plugin | updating readme · CakeDC/search@668eb68 · GitHub |
*1:#1241 - Operand should contain 1 column(s)