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;
Related
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
)
)
),
));
I have this inner join query in a paginated model called Application
Application has many AssignedExploration
AssignedExploration belongs to Exploration
Exploration belongs to ExplorationCategory
ExplorationCategory belongs to Season
My query so far is:
$session_condition = array(
'table' => 'assigned_explorations',
'alias' => 'AssignedExploration',
'type' => 'INNER',
'conditions' => array(
'AssignedExploration.application_id = Application.id',
'OR' => array(
array('AssignedExploration.label' => $this->request->named['filter_session'])
)
)
);
I would like to add another query inside the OR that will get applications which has a Season.name = fall
I've tried adding array('Season.name' => 'fall') :
$session_condition = array(
'table' => 'assigned_explorations',
'alias' => 'AssignedExploration',
'type' => 'INNER',
'conditions' => array(
'AssignedExploration.application_id = Application.id',
'OR' => array(
array('AssignedExploration.label' => $this->request->named['filter_session']),
array('Season.name' => 'fall')
)
)
);
But no luck. Seems like Season is not recognized by AssignedExploration.
I would like to nest from AssignedExploration to Exploration to ExplorationCategory and to Season
And I don't have any idea on how to make it thru inner join query via cakephp.
Thanks in advanced.
EDIT: query as of now:
INNER JOIN
`ntc_development`.`assigned_explorations` AS `AssignedExploration` ON (
`AssignedExploration`.`application_id` = `Application`.`id`
AND
`AssignedExploration`.`label` = 'fall'
)
$this->paginate['Application'] = array(
'recursive' => -1,
'joins' => array(
array(
'table' => 'assigned_explorations',
'alias' => 'AssignedExploration',
'type' => 'inner',
'foreignKey' => true,
'conditions' => array('AssignedExploration.application_id = Application.id')
),
array(
'table' => 'seasons',
'alias' => 'Season',
'type' => 'inner',
'foreignKey' => true,
'conditions' => array('Season.id = Application.season_id') # whats your foregin key
),
),
'fields' => array(),
'conditions' => array('Season.name' => 'fail'),
);
When below code below it generate query with single quote in join query
$this->Paginator->settings = array(
'joins' => array(
array(
'table' => 'businesses_categories',
'alias' => 'BusinessesCategory',
'type' => 'LEFT',
'conditions' => array('Business.id' => 'BusinessesCategory`.`business_id'),
),
array(
'table' => 'categories',
'alias' => 'Category',
'type' => 'LEFT',
'conditions' => array('BusinessesCategory.category_id' => 'Category.id'),
),
),
'conditions' => array(
'Category.id' => 24),
'limit' => 10
);
$businesses = $this->Paginator->paginate('Business');
I have added query that's generated by above paginator query. query works fine, when i use ON (Business.id = BusinessesCategory.business_id) instead of ON (Business.id = 'BusinessesCategory.business_id')
how do i fix this. so, it does not include single quote on values
SELECT `Business`.`id`,
`Business`.`state`,
`Business`.`slug`,
`Business`.`city`,
`Business`.`suburb`,
`Business`.`user_id`,
`Business`.`business_name`,
`Business`.`business_address`,
`Business`.`business_postal`,
`Business`.`business_postal_id`,
`Business`.`business_phone`,
`Business`.`business_phone1`,
`Business`.`business_phone2`,
`Business`.`business_email`,
`Business`.`business_website`,
`Business`.`business_details`,
`Business`.`business_openinghours`,
`Business`.`business_service`,
`Business`.`business_addtionalinfo`,
`Business`.`business_lat`,
`Business`.`business_lng`,
`Business`.`identity`,
`Business`.`status`
FROM `yuldicom`.`businesses` AS `Business`
LEFT JOIN `yuldicom`.`businesses_categories` AS `BusinessesCategory` ON (`Business`.`id` = 'BusinessesCategory`.`business_id')
LEFT JOIN `yuldicom`.`categories` AS `Category` ON (`BusinessesCategory`.`category_id` = 'Category.id')
WHERE `Category`.`id` = 24 LIMIT 10
Here you go :--
$this->Paginator->settings = array(
'joins' => array(
array(
'table' => 'businesses_categories',
'alias' => 'BusinessesCategory',
'type' => 'LEFT',
'conditions' => array('Business.id=BusinessesCategory.business_id'),
),
array(
'table' => 'categories',
'alias' => 'Category',
'type' => 'LEFT',
'conditions' => array('BusinessesCategory.category_id=Category.id'),
),
),
'conditions' => array(
'Category.id' => 24),
'limit' => 10
);
$businesses = $this->Paginator->paginate('Business');
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")
)
)
);
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);