Skip to content

Instantly share code, notes, and snippets.

@rynop
Created April 12, 2011 18:21
Show Gist options
  • Save rynop/916076 to your computer and use it in GitHub Desktop.
Save rynop/916076 to your computer and use it in GitHub Desktop.
<?php
class Foo extends AppModel {
var $name = 'Foo';
var $virtualFields = array(
'tot_impressions' => 'COALESCE(SUM(Foo.impressions),0)',
'tot_clicks' => 'COALESCE(SUM(Foo.clicks),0)', //This is initial ad clicks
);
....
return $this->Account->find('all', array(
'oneQuery'=>array(
'Foo'=>array(
'fields'=>array(
'id',
'Foo.tot_impressions',
'Foo.tot_clicks',
)
)
),
'fields' => array('Account.name','Account.id'),
'group'=>'Account.id',
'order'=>'Account.name'
));
Generates:
SELECT `Account`.`name`, `Account`.`id`, `Foo`.`id`, `Foo`.`tot_impressions`, `Foo`.`tot_clicks` FROM `accounts` AS `Account` LEFT JOIN `ad_data` AS `Foo` ON (`Foo`.`account_id` = `Account`.`id`) GROUP BY `Account`.`id` ORDER BY `Account`.`name` ASC
Should be generating:
SELECT `Account`.`name`, `Account`.`id`, `Foo`.`id`, (COALESCE(SUM(`Foo`.`impressions`),0)) AS `Foo__tot_impressions`, (COALESCE(SUM(`Foo`.`clicks`),0)) AS `Foo__tot_clicks`....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment