virtualFieldæ©è½ä½¿ã£ã¦ã¿ã
shin1x1ãããtwitterã§ãver1.3ã«ãããªæ©è½ããããã¨twitterで呟かれていたã®ãã¿ã¦ãå®éã«ä½¿ã£ã¦ã¿ã¾ããã
virtualFieldæ©è½ã§ãã§ãããã¨*1
ã¾ãã¯ãä½æããMemberã¢ãã«ã«ã元記事ãåèã«ã$virtualFieldãè¨å®ãã¦è¦ã¾ã*2ã
app/model/members.php
var $virtualFields = array( 'name' => 'CONCAT(Member.first_name, " ", Member.family_name)' );
ããã¦ãbakeã§ä½ã£ãindex(membersä¸è¦§ï¼ã«ã¢ã¯ã»ã¹ããã¨ããããªSQLãåºåããã¦ãã¾ããã
SELECT `Member`.`id`, `Member`.`first_name`, `Member`.`family_name`, `Member`.`created`, `Member`.`modified`, (CONCAT(`Member`.`first_name`, " ", `Member`.`family_name`)) AS `Member__name` FROM `members` AS `Member` WHERE 1 = 1 LIMIT 20
ä¸è¦ãªãã¨ã«ãç§ãSQLã¯ãã¾ãè¤éãªä½¿ãæ¹ã¯ãã¦ããªãã£ãããããCONCATãã¨ãã表ç¾ã«ãã³ã¨ãã¦ã¾ããã§ããã
ãããSQLã®æååé¢æ°ã§ãï¼ï¾ãï¾;ï¼
ã¤ã¾ããvirtualFieldã使ãã¨ã
SELECTã®ãã£ã¼ã«ãæå®é¨åã«ãããããèªç±ã«ã»ããã§ããã¨ãããã¨ï¼
試ãã«ãéãé¢æ°ãå ¥ãã¦ã¿ãã
var $virtualFields = array( 'name' => 'CONCAT(Member.family_name, " ", Member.first_name)', 'date' => 'DATE(Member.created)' // 追å );
ããã¦ãå度index()ã«ã¢ã¯ã»ã¹ã
SELECT `Member`.`id`, `Member`.`first_name`, `Member`.`family_name`, `Member`.`created`, `Member`.`modified`, (CONCAT(`Member`.`family_name`, " ", `Member`.`first_name`)) AS `Member__name`, (DATE(`Member`.`created`)) AS `Member__date` FROM `members` AS `Member` WHERE 1 = 1 LIMIT 20
çµæãvar_dumpã§åºåãã¦ã¿ãã
array(1) {
[0]=> array(1)
{ ["Member"]=> array(7)
{ ["id"]=> string(1) "1"
(ä¸ç¥ï¼
["name"]=> string(13) "åæ± å¤ªé"
["date"]=> string(10) "2010-01-20"
ããã¼ ^^
AS `Member__name`
ã¨ããã«ã¢ã³ãã¼ã¹ã³ã¢ã§åç §ããã¦ãã®ã§å¤æ°åå¿é ã ã£ããã§ãããæ°ã«ããªãã¦OKã
ããã«è©¦ãã«ãmembersãã¼ãã«ã«ããmoneyï¼ææéï¼ããªãã¦ã«ã©ã ã追å ãã¦ãæ°å¤è¨ç®ã¨ããã¦ã¿ãã
mysql> ALTER TABLE members ADD COLUMN money INT(11) NOT NULL default '0'; Query OK, 1 row affected (0.05 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> quit Bye [cake@cake app]$ cd ../ [cake@cake cakephp-13b]$ vi app/views/members/index.ctp (以ä¸ç¥ãindexã¨add,editã«nameã¨moneyã®è¡¨ç¤ºè¿½å ï¼
ä¸è¦§ã«ãnameã¨moneyãã§ãããã«ãªã£ãã¨ããã§ããã¼ã¿è¿½å ã
ããã¦ãåãfamily_nameã®ã¡ã³ãã¼ã®moneyãåç®ãã¦ä¸è¦§è¡¨ç¤ºããã¢ã¯ã·ã§ã³ãä½æã
app/controller/members_controller.php
function grouped_list() { $this->Member->virtualFields['money_sum'] = 'SUM(Member.Money)'; $list = $this->Member->find('all', array( 'group' => 'Member.family_name', ) ); $this->set('members', $list); }
ã§ãã¾ããï¼ (^^)
ãã£ã¼ã«ãã«èªç±ã«ã»ããã§ããã®ã§ã使ãæ¹æ¬¡ç¬¬ã§ããããªãã¨ãåºæ¥ããã§ãã