VitualFieldsã使ããï¼
CakePHP1.3ããã¯ãVirtualFieldsãå©ç¨ã§ããã
ããã¯SQL ã§ãã£ãã sql_function(hoge) as aliasã¨ãã£ãcolmunããã®ã¾ã¾ãã£ã¼ã«ãã¨ãã¦å©ç¨ã§ããæ©è½ã ã
ããã§ã¯å®ä¾ã追ããªããé 次説æãã¦ãããã
è¦æ±ï¼
ã°ã«ã¼ãã®ä¸è¦§ã表示ãããã
ã¾ããã°ã«ã¼ãã«ã¯ä½äººåå ãã¦ããããéè¨ãã¦åæã«è¡¨ç¤ºããã
é¢ä¿
Group hasAndBelongsToMany User
Model
<?php class Group extends AppModel { var $name = 'Group'; var $virtualFields = array( 'user_count' => 'count(GroupsUser.user_id)', ); var $hasAndBelongsToMany = array( 'User', ); } ?>
Modelã®ãã£ã¼ã«ãã¨ãã¦ã$virtualFieldsã宣è¨ããããã®ä¸ã«virtualFieldsã¨ãã¦è¨å®ããããã®ãæ¸ãã¦ããã
ä¸è¨ä¾ã®ããã«ã'ã¨ã¤ãªã¢ã¹å' => 'SQLã®ã«ã©ã æå®æ§æ'ãè¨è¿°ãã¦ããã
Controller
<?php class GroupsController extends AppController { function index() { $this->Group->recursive = 0; $this->paginate = array( 'Group' => array( 'joins' => array( array( 'type' => 'LEFT', 'alias' => 'GroupsUser', 'table' => 'groups_users', 'conditions' => 'Group.id = GroupsUser.group_id' ) ), 'group' => array( 'Group.id' ), 'fields' => array( 'Group.id', 'Group.name', 'user_count' // vitualFields! ) ), ); $this->set('groups', $this->paginate()); // var_dump($this->viewVars['groups']); } ?>
ãã¦ãoptionsã®fieldsãorderãgroupçã§virtualFieldsãæå®ããã¨ãã¯ãã¢ãã«åãå«ããªãã
ã¤ã¾ã
- 'Model.virtual_fields'
ã§ã¯ãªããåã«
- 'virtual_fields'
ã¨æå®ããã
ã¢ãã«åãå«ããã¨ãé常ã®Fieldsã¨è§£éãããDBMã«ããããªã«ã©ã ãã¼ããã·ããããã¨æããã¦ãã¾ãã
ãªãã§ãããã£ã¦æã人ã¯dbo_source.phpã§"virtual"ã§æ¤ç´¢ãã¦ã¿ãã¨åã¿è¾¼ãããããããªãã
ä»å¾ãModelåã¤ãã§æå®ã§ããããã«ãªããã¯ä¸æã ã
âä¿®æ£ããã¾ããVirtualFieldsで、'Model.name'が指定できないバグãåç
§ãã¦ãã ããã
ãã¨éè¦ãªã®ã¯ãoptions[fields]ãæå®ããªãå ´åãã¤ã¾ãModel.*ã*.*ãåå¾ããå ´åãVirtualFieldsãå«ã¾ãããã¨ã«æ³¨æã
View
<table cellpadding="0" cellspacing="0"> <tr> <th><?php echo $paginator->sort(__('åå',true),'name');?></th> <th><?php echo $paginator->sort(__('åå ã¦ã¼ã¶',true),'user_count'); /* Virual Fields!! */ ?></th> </tr> <?php $i = 0; foreach ($groups as $group): $class = null; if ($i++ % 2 == 0) { $class = ' class="altrow"'; } ?> <tr<?php echo $class;?>> <td> <?php echo $html->link($group['Group']['name'], array('action' => 'view', $group['Group']['id'])) ?> </td> <td> <?php echo ife($group['Group']['user_count'],sprintf(__('%d人',true),$group['Group']['user_count']),__('ãªã',true)) ?> </td> </tr> <?php endforeach; ?> </table>
æå¾ã«ãã¥ã¼ã§ã¯é¢ç½ããã¨ã«ãªã£ã¦ããã
$paginator->sort()ã¯ãorderããFieldãæå®ã§ããããVirtualFieldãæå®ã§ããããã«ãªã£ãã
ããããå®éã«è¡¨ç¤ºããã¨ãããè¦ã¦ãããããã
<?php $group['user_count'] ?>
ã§ã¯ãªãã
<?php $group['Group']['user_count'] ?>
ã¨ãªã£ã¦ãããã¤ã¾ãfind()ã®è¿ãå¤ã¨ãã¦ã¯ãGroupã¢ãã«ã¨ãã¦èªèããã¦æ ¼ç´ããã¦ããããã ã
1.2以åã§aliasãæå®ããå ´åã¯ã
<?php $group[0]['user_count'] ?>
ã¨ããªããã°ãªããªãã£ãããããä¸æ£è§£ã¨ãªãã
æå¾ã«åãåºãããSQLãè¦ã¦ã¿ããã
SQL
ã¡ãªã¿ã«user_countã§ã½ã¼ãããæã®SQLã«ãªãã
SELECT `Group`.`id`, `Group`.`name`, (count(`GroupsUser`.`user_id`)) AS `Group__user_count` FROM `groups` AS `Group` LEFT JOIN groups_users AS `GroupsUser` ON( `Group`.`id` = `GroupsUser`.`group_id` ) WHERE 1 = 1 GROUP BY `Group`.`id` ORDER BY (count(`GroupsUser`.`user_id`)) asc LIMIT 20
ï¼è¦ãããããã«é©å½ã«æ´å½¢ããï¼
ã¨ã¤ãªã¢ã¹å => ã¢ãã«å__VirtualFieldså
ã¨ãªã£ã¦ããããORDER BYå¥ã§ã¯ã¨ã¤ãªã¢ã¹ã使ç¨ãã¦ããªãã
ä½ãæå³ããããã¯åãããªããããã£ã¦æ¬²ãããã¨ã¯ååæºããããã ã
ã¾ã¨ãã»ä»å¾
VirtualFieldsã¯ã¯ã¼ã«ã ã
éè¨é¢æ°ã¯å¿
ãVirualFieldsã¨ãã¦å®£è¨ãã¦ãã¾ãã°ãéè¨é¢æ°ãã³ã¼ãã®ãããä¸ã«æ£ãã°ã£ã¦ã¡ã³ããã³ã¹ãé£ãããªããã¨ãåé¿ã§ããã
æ´ã«paginatorãcontainã«å«ãããã¨ã«ãã£ã¦ãã³ã¼ãã£ã³ã°ãä¸è²«ããé常ã«æ¥½ã«ãªãã
ããã¨ãã£ã¨ããéã«éè¨é¢æ°ã®ã½ã¼ãä»ããã¼ã¸ä»ããå®è£
ã§ããã®ã¯é常ã«æ°æã¡ãããã
æ¯éãä»ãã1.3ã«ç§»è¡ãã¦ãï¼ãããã¯ç§»è¡ãã¦ã人ããï¼VirtualFieldsã使ã£ã¦ã¿ã¦æ¬²ããã
ä»å¾ã¯validateã«å¿ç¨ã§ããªããçãç´°ããæªæ¤è¨¼ãªç¹ããã£ããéæè¨äºã«ãã¦è¡ãããã¨æãã
ãã¾ã
1.2ã§paginatorã«éè¨é¢æ°ã渡ããªãï¼ã·ãããã£ã¦äººã«ãèªåããã¦ã¦ãããããç´¹ä»ã
AppControllerã«ãã®ã¾ã¾è²¼ãã°ï¼å¤åï¼åä½ãã¾ãã
http://bin.cakephp.org/view/1057852525
使ãæ¹ã¯ã$fields = array(
'sum_function(A.hoge) as alias'
)
'as' ãå¿
é ãªã®ã«æ³¨æã
å¾ã¯aliasé¨åã$paginator->sort()ã«æ¸¡ãã¾ãã