問題描述
Cakephp 複雜查詢 (Cakephp complex query)
I have the sql code and i want to do in cake php but i dont know how and i don't want to do it like that
$obj->query($sql);This is my query:
"SELECT dbfiles.amount, MONTHNAME(cases.enquiry_date) as Month FROM cases INNER JOIN services ON cases.id = services.case_id INNER JOIN dbfiles ON services.id = dbfiles.service_id WHERE cases.status = 2 AND YEAR(cases.enquiry_date) = '2012' AND dbfiles.type = 'INV' AND cases.currency = 'EUR' GROUP BY dbfiles.invoice_num ORDER BY Month DESC; "
參考解法
方法 1:
Assuming $obj is of class Case
$obj->find('all', array(
'fields' => array('dbfiles.amount', 'MONTHNAME(cases.enquiry_date) as MONTH',
'joins' => array(
array(
'table' => 'services',
'type' => 'INNER',
'conditions' => array('cases.id = services.case_id'),
'foreignKey' => false
),
array(
'table' => 'dbfiles',
'type' => 'INNER',
'conditions' => array('services.id = dbfiles.service_id'),
'foreignKey' => false
)
),
'conditions' => array('cases.status' => 2, ... the rest of your WHERE clauses)
)
(by zihatzik、Andreas Wong)