CakePHP and GROUP BY - php

Using CakePHP 1.2, I am trying produce a GROUP BY query:
SELECT `categories`.*, COUNT(`entities`.id)
FROM `categories`
LEFT JOIN `entities` ON (`categories`.`id` = `entities`.`category_id`)
GROUP BY `categories`.`id`
How would/should I go about doing this? I am using 'Containable' if that helps.

This is what I eneded up with:
$options = array(
'conditions' => $conditions,
'fields'=>array('Category.*','COUNT(`Entity`.`id`) as `entity_count`'),
'joins' => array('LEFT JOIN `entities` AS Entity ON `Entity`.`category_id` = `Category`.`id`'),
'group' => '`Category`.`id`',
'contain' => array('Domain' => array('fields' => array('title')))
);
return $this->find('all', $options);

Model->find() has a group param.

Possible keys by default, all of which are optional:
$params =
array(
'conditions' => array('Model.field' => $thisValue), //array of conditions
'recursive' => 1, //int
//array of field names
'fields' => array('Model.field1', 'DISTINCT Model.field2'),
//string or array defining order
'order' => array('Model.created', 'Model.field3 DESC'),
'group' => array('Model.field'), //fields to GROUP BY
'joins' => array('Join relations here'), // for ex: LEFT JOIN `entities` AS Entity ON `Entity`.`category_id` = `Category`.`id`
'limit' => n, //int
'page' => n, //int
'offset' => n, //int
'callbacks' => true //other possible values are false, 'before', 'after'
);
Query:
$resp = find('all', $params);
debug($resp);

Related

CakePHP with Joins inner

I need to do the following query using the CakePHP find method:
SELECT * FROM `ads`
INNER JOIN fields_values ON fields_values.ref_id = ad.id
WHERE ad.active = 1
I tried this function, but she doesn't work :
$ads = $this->Ad->find('all', array(
'joins' => array(
array(
'table' => 'fields_values',
'alias' => 'fv',
'type' => 'INNER',
'conditions' => array(
"Ad.id = fv.ref_id "
)
)
),
'conditions' => array(
'Ad.active' => 1
),
));
your query is wrong it should be
SELECT * FROM `ads`
INNER JOIN fields_values as fv ON fv.ref_id = ads.id
WHERE ads.active = 1
Now you can build it up with cake query. Which left join also does it.
$ads = $this->Ad->find('all', array(
'conditions' => array(
'Ad.active' => 1
),
'joins' => array(
array(
'table' => 'fields_values',
'alias' => 'fv',
'type' => 'LEFT',
'conditions' => array(
"fv.ref_id = Ad.id",
// or you can add the top condition here
//'Ad.active' => 1
)
)
),
));

left join on cake php 1.3

How to create this query on cakePHP 1.3
SELECT "Redeem_log"."benefit_id", count("Redeem_log"."benefit_id") as JUMLAH FROM "redeem_logs" "Redeem_log"
LEFT JOIN "benefits" "Benefit" ON ("Redeem_log"."benefit_id" = "Benefit"."id")
LEFT JOIN "merchants" "Merchant" ON ("Benefit"."merchant_id" = "Merchant"."id")
LEFT JOIN "merchant_types" "Merchant_type" ON ("Merchant"."merchant_type_id" = "Merchant_type"."id")
WHERE "Redeem_log"."benefit_id" IS NOT NULL AND ("Merchant_type"."merchant_type"='lokal' OR "Merchant_type"."merchant_type"='nasional') GROUP BY "Redeem_log"."benefit_id" ORDER BY "JUMLAH" DESC
I don't want to use belongsTO, hasMany or ect
If I use
var $belongsTo = array(
'Benefit' => array('className' => 'Benefit', 'foreignKey' => 'benefit_id'),
'Merchant' => array('className' => 'Merchant', 'foreignKey' => 'merchant_id')
);
left join is like :
SELECT "Redeem_log"."benefit_id", count("Redeem_log"."benefit_id") as JUMLAH
FROM "redeem_logs" "Redeem_log"
LEFT JOIN "benefits" "Benefit" ON ("Redeem_log"."benefit_id" = "Benefit"."id")
LEFT JOIN "merchants" "Merchant" ON ("Redeem_log"."merchant_id" = "Merchant"."id")
WHERE "Redeem_log"."benefit_id" IS NOT NULL GROUP BY "Redeem_log"."benefit_id"
ORDER BY "JUMLAH" DESC
I would do it this way, the params for the query looks bit scary, but when you look on it in details it makes sense:
$params = array(
'recursive' => -1,
'fields' => array('RedeemLog.benefit_id', 'COUNT(RedeemLog.benefit_id) as JUMLAH'),
'conditions' => array(
array('NOT' => array('RedeemLog.benefit_id' => null)),
array('Merchanttype.merchant_type' => array('lokal', 'nasional')),
),
'joins' => array(
array(
'table' => 'benefits',
'alias' => 'Benefit',
'type' => 'LEFT',
'conditions' => array('RedeemLog.benefit_id = Benefit.id')
),
array(
'table' => 'merchants',
'alias' => 'Merchant',
'type' => 'LEFT',
'conditions' => array('Benefit.merchant_id = Merchant.id')
),
array(
'table' => 'merchant_types',
'alias' => 'Merchanttype',
'type' => 'LEFT',
'conditions' => array('Merchant.merchant_type_id = Merchanttype.id')
),
),
'order' => 'JUMLAH DESC',
'group' => 'RedeemLog.benefit_id',
);
and finaly the cake find
$result = $this->RedeemLog->find('all', $params);
hope it works for you.
Add joins field in params parameter of MOdel::find();
like this
$params = array(
'joins'=>array(
array(
'table'=>'users',
'alias'=>'User',
'type'=>'LEFT/RIGHT/INNER',
'conditions'=>array("$this->Alias.foreignKey=User.pk")
)
)
);

How to make query joins in cakePHP?

I just wanna ask how to make query joins in CakePHP following this SQL syntax
SELECT a.id, SUM( r.jumlah_realisasi) AS jumlah_realisasi, SUM(b.jumlah_budget) AS jumlah_budget, SUM(c.jumlah_contrapos)
FROM accountposts a
LEFT JOIN
(
SELECT accountpost_id, agency_id, SUM(budget_after_changes) AS jumlah_budget
FROM budgets
GROUP BY accountpost_id
) b
ON a.id = b.accountpost_id
LEFT JOIN
(
SELECT accountpost_id, agency_id, SUM(realisation_value) AS jumlah_realisasi
FROM realisations
GROUP BY accountpost_id
) r
ON a.id = r.accountpost_id
LEFT JOIN
(
SELECT accountpost_id, agency_id, SUM(contrapos_value) AS jumlah_contrapos
FROM contraposts
GROUP BY accountpost_id
) c
ON a.id = c.accountpost_id
GROUP BY
a.id
And I tried with this syntax (I use CakePHP 2.x):
$joins = array(
array(
'table' => 'budgets',
'alias' => 'Budget',
'type' => 'LEFT',
'conditions' => array('Accountpost.id = Budget.accountpost_id')
),
array(
'table' => 'realisations',
'alias' => 'Realisation',
'type' => 'LEFT',
'conditions' => array('Accountpost.id = Realisation.accountpost_id')
),
array(
'table' => 'contraposts',
'alias' => 'Contrapost',
'type' => 'LEFT',
'conditions' => array('Accountpost.id = Contrapost.accountpost_id')
),
);
$this->paginate = array(
'limit' => 60,
'joins' => $joins,
'fields' => array('Accountpost.id','Accountpost.explanation','Accountpost.account_code',
'SUM(Budget.budget_after_changes) AS jumlah_budget','SUM(Realisation.realisation_value) AS jumlah_realisasi','SUM(Contrapost.contrapos_value) AS jumlah_contrapos'),
'group' => array('Accountpost.id'),
'order' => array('Accountpost.id' => 'ASC'),
);
And here is the SQL Dump from CakePHP :
SELECT `Accountpost`.`id`, `Accountpost`.`explanation`, `Accountpost`.`account_code`, SUM(`Budget`.`budget_after_changes`), `Budget`.`budget_after_changes`, `Realisation`.`realisation_value`, `Contrapost`.`contrapos_value` FROM `realisasi_anggaran`.`accountposts` AS `Accountpost` LEFT JOIN `realisasi_anggaran`.`budgets` AS `Budget` ON (`Accountpost`.`id` = `Budget`.`accountpost_id`) LEFT JOIN `realisasi_anggaran`.`realisations` AS `Realisation` ON (`Accountpost`.`id` = `Realisation`.`accountpost_id`) LEFT JOIN `realisasi_anggaran`.`contraposts` AS `Contrapost` ON (`Accountpost`.`id` = `Contrapost`.`accountpost_id`) WHERE 1 = 1 GROUP BY `Accountpost`.`id` ORDER BY `Accountpost`.`id` ASC LIMIT 60
But the result is different between SQL syntax version and CakePHP version, in SQL syntax there is no duplicated values while examining SUM, but in CakePHP version there are duplicated values while examining SUM. How do I implement my SQL syntax to cakePHP the right way?
Your $options array is incorrectly constructed. This way the array has two nested, named keys called joins and Cake will not construct a correct SQL join.
The arrays should either look like this:
$joins = array(
array(
'table' => 'budgets',
'alias' => 'Budget',
'type' => 'LEFT',
'conditions' => array('Accountpost.id = Budget.accountpost_id')
)
);
$this->paginate = array(
'limit' => 60,
'joins' => $joins,
'fields' => array('Accountpost.id','Accountpost.explanation','Accountpost.account_code',
'SUM(Budget.budget_after_changes) AS jumlah_budget'),
'group' => array('Accountpost.id'),
'order' => array('Accountpost.id' => 'ASC'),
);
Or be constructed like the following:
$options = array(
'limit' => 60,
'fields' => array('Accountpost.id','Accountpost.explanation','Accountpost.account_code',
'SUM(Budget.budget_after_changes) AS jumlah_budget'),
'group' => array('Accountpost.id'),
'order' => array('Accountpost.id' => 'ASC'),
);
$options['joins'] = array(
array(
'table' => 'budgets',
'alias' => 'Budget',
'type' => 'LEFT',
'conditions' => array('Accountpost.id = Budget.accountpost_id')
)
);
$this->paginate = $options;

CakePHP SQL Error

Im trying to do a purge of records in a database though when I made my query and associations it seems to not want to do it correctly. I got the following error and Im confused as to why this is occurring:
SQL Error: 1054: Unknown column 'GuardiansStudents.student_id' in 'where clause'
The query that gets displayed afterwards is the following:
Query: SELECT `User`.`id`, `Guardian`.`id`
FROM `guardians` AS `Guardian`
LEFT JOIN `users` AS `User` ON (`Guardian`.`user_id` = `User`.`id`)
WHERE `GuardiansStudents`.`student_id` IS NULL
AND `User`.`active` = 1 AND `User`.`changeapprovalneeded` = 0
I also have the following associations in the Guardian model, not sure if Im doing this properly, and possibly this is where the error is occurring:
class Guardian extends AppModel {
var $name = 'Guardian';
//The Associations below have been created with all possible keys,
// those that are not needed can be removed
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
var $hasAndBelongsToMany = array(
'Student' => array(
'className' => 'Student',
'joinTable' => 'guardians_students',
'foreignKey' => 'guardian_id',
'associationForeignKey' => 'student_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
This is the code to do the purge function:
function manager_purgebygrade() {
ini_set('max_execution_time','120');
$this->layout = "manager";
$this->User->recursive = 0;
$grades = $this->User->Student->Grade->getDropDownList();
$this->set(compact('grades'));
//debug($this->data);
if(!empty($this->data['User']['grade_id']))
{
//$this->User->bindModel(array())
$users = $this->User->find(
'list',
array(
'fields' => array(
'User.id'
),
'conditions' => array(
'Student.grade_id' => $this->data['User']['grade_id']
),
'recursive' => 0
)
);
//debug($users);
$this->User->deleteAll(array('User.id' => $users), true);
$this->User->Guardian->bindModel(
array('hasOne' => array('GuardiansStudents')));
$guardianswithnostudents = $this->Guardian->deleteGuardiansWithNoStudent();
$guardians = $this->User->Guardian->find(
'list',
array(
'fields' => array(
'User.id',
'User.id'
),
'conditions' => array(
'GuardiansStudents.student_id' => null,
'User.active' => 1,
'User.changeapprovalneeded' => 0
),
'recursive' => 1
)
);
$this->User->deleteAll(array('User.id' => $guardians), true);
$this->set(compact('users','guardians','guardianswithnostudents'));
}
}
Hopefully someone can point me in the right direction I would greatly appreciate it :).
Your where clause:
WHERE `GuardiansStudents`.`student_id` IS NULL
is using table GuardiansStudents that appears nowhere in the from clause or join clause.
the model is GuardiansStudent, not GuardiansStudents.

CakePHP: joins inside contain

As the title says, I'm having troubles with joins in my find-query, with errors appearing like:
preg_match() expects parameter 2 to be string, array given [CORE/cake/libs/model/behaviors/containable.php, line 301]
I've tried adding joins to the array over there, but it didn't change anything.
These are the options I'm passing to the find method.
$options = array(
'contain' => array(
'Answer' => array(
'conditions' => array('Answer.type' => 'answer'),
'joins' => array(
$this->votesJoin()
),
'Comment' => array(
'conditions' => array('Comment.type' => 'comment'),
'joins' => array(
$this->votesJoin()
)
)
),
'Comment' => array(
'conditions' => array('Comment.type' => 'comment'),
'joins' => array(
$this->votesJoin()
)
),
'User',
'Tag' => array()
),
'joins' => array(
$this->votesJoin()
),
'conditions' => array(
'Question.id' => $id
)
);
return $this->find('first', $options);
with votesJoin() returning the following array.
(
[table] => (SELECT Vote.node_id, SUM(value) as total FROM votes AS `Vote` WHERE 1 = 1 GROUP BY `Vote`.`node_id` )
[alias] => Vote
[conditions] => Vote.node_id = Question.id
)
What I'm trying to do:
Each user can up/downvote a node (question/answer/comment). With the join I'm trying to add the sum of those votes.
database http://github.com/navale/QA/wiki/img/datamodel.png
You should use "joins" only for things that can't be done with Cake model relationship and Containable. I don't know the details of your database, but I think the find operation can be simplified. Why don't you post the schema for these tables on here?
try this:
$options = array(
'contain' => array(
'Answer' => array(
'conditions' => array('Answer.type' => 'answer'),
'Vote' => array(
'fields' => array('SUM(Vote.value)'),
'group' => array('Vote.parent_id')
),
'Comment' => array(
'conditions' => array('Comment.type' => 'comment'),
'Vote' => array(
'fields' => array('SUM(Vote.value)'),
'group' => array('Vote.parent_id')
)
)
),
'Comment' => array(
'conditions' => array('Comment.type' => 'comment'),
'Vote' => array(
'fields' => array('SUM(Vote.value)'),
'group' => array('Vote.parent_id')
)
),
'User',
'Tag' => array()
),
'conditions' => array(
'Question.id' => $id
)
);
You get the Vote sum value for each answer, comment, and comment for answer. (You might need to add 'hasMany' Vote in the Node model if you haven't done that yet)
If instead you want to get one single total sum of Vote for the question, then I'd suggest:
get the list of the answers and comments of the question:
$lvl1 = find('list','fields'=>array('id'),'conditions'=>array('Node.parent_id'=>$id))
then get list of the comments of the answers
$lvl2 = find('list','fields'=>array('id'),'conditions'=>array('Node.parent_id'=>$lvl1))
then just combine the 2 array then do a sum over that.

Categories