Skip navigation

If you need to sum up or average a column on a table via bindModel. this is what you are looking for.
The table looks like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//each report is connected to same session report via session_id

CREATE TABLE IF NOT EXISTS `gather_reports` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `session_id` varchar(64) NOT NULL DEFAULT '',
  `type` enum('Category','Store') NOT NULL DEFAULT 'Category',
  `name` varchar(128) NOT NULL DEFAULT '',
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `importedId` text NOT NULL,
  `total` int(11) NOT NULL DEFAULT '0',
  `imported` int(11) NOT NULL DEFAULT '0',
  `duration` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
)

in your controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
$this->GatherReport->bindModel(
    array('hasOne'=>array(
	'GatherTotal'=>array(
        	'className'=>'GatherReport',
		'foreignKey'=>'id',
		'fields'=>'SUM(GatherTotal.total) as total'
	),
	'GatherImported'=>array(
		'className'=>'GatherReport',
		'foreignKey'=>'id',
		'fields'=>'SUM(GatherImported.imported) as imported'
	),
	'GatherTime'=>array(
		'className'=>'GatherReport',
		'foreignKey'=>'id',
		'fields'=>'SUM(GatherTime.duration) as duration'
	)
)),false);
$this->paginate['GatherReport']['group'] = 'GatherReport.session_id';
$data = $this->paginate('GatherReport');

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>