CakePHP2ã®Paginateã§CASEå¼ãªã©ã使ã£ããã£ã¼ã«ããæå®ãã¦ã½ã¼ãããæ¹æ³
CakePHP2ã§ãCASEãCONCATãªã©ã使ã£ã¦ãã¼ãã«ã«åå¨ããªããã£ã¼ã«ãåã§ã½ã¼ããè¡ãå ´åãä¾ãã°ä¸è¨ã®ããã«ããã¨æãã¾ãã
<?php $params = [ 'fields' => ['CASE WHEN User.age >= 20 then 1 else 0 END AS adult'], 'order' => ['adult DESC'] ]; find('all', $params);
é常ã®findã§ããã°åé¡ãªãã§ããããã¼ã¸ã³ã°ã®orderã§ãã®ãã£ã¼ã«ããæå®ãã¦ãå¹ãã¦ããã¾ããã
ãã®ãããªå ´åã¯ãã¢ãã«ã®ãã¼ãã£ã«ãã£ã¼ã«ããå©ç¨ãã¾ãã
<?php //ã³ã³ããã¼ã©ã®ä¸ã§Userã¢ãã«ãå©ç¨ $this->User->virtualFields['adult'] = 'CASE WHEN User.age >= 20 then 1 else 0 END'; $this->Paginator->settings = ['order' => ['User.adult' => 'desc']];
åè:
http://book.cakephp.org/2.0/ja/models/virtual-fields.html
"ãã¼ãã£ã«ãã£ã¼ã«ã㯠find æã«æ®éã®ãã£ã¼ã«ãã¨åãããã«æ¯èããããController::paginate() ã¯ãã¼ãã£ã«ãã£ã¼ã«ãã§ãã½ã¼ããããã¨ãã§ãã¾ãã"
http://stackoverflow.com/questions/21160171/implement-order-by-case-with-paginator-in-cakephp