首先,这是我的database relation diagram。
我正在尝试使用每天订购的某个元素的数量制作图表
date | count
2011-04-22 | 10348
2011-04-23 | 2751
由于我对SQL比较熟悉,我首先就是这样,这就是最终出来的(并且有效)
SELECT dn.delivery_date, SUM(dnp.count*ep.count) as count
FROM
`delivery_notes` AS dn JOIN `delivery_notes_products` AS dnp
ON dn.id = dnp.delivery_note_id
JOIN `products` AS p
ON dnp.product_id = p.id
JOIN `elements_products` AS ep
ON p.id = ep.product_id
JOIN `elements` AS e
ON ep.element_id = e.id
WHERE e.id = 4
AND dn.delivery_date BETWEEN '2011-04-22' AND '2011-05-22'
GROUP BY dn.delivery_date
现在改进我的获得一些Cake-find技能,我试图理解如何将其变成find('list')或find('all')查询,但是我真的不知道我应该如何解决这个......
非常感谢任何帮助。
答案 0 :(得分:0)
$this->DeliveryNote->bindModel(array(
'hasOne'=>array(
'DeliveryNotesProduct'=>array(
'conditions'=>array('DeliveryNotesProduct.DeliveryNoteId = DeliveryNote.id')
),
'Product'=>array(
'conditions'=>array('DeliveryNotesProduct.ProductId = Product.id')
),
'ElementsProduct'=>array(
'conditions'=>array('ElementsProduct.ProductId = Product.id')
),
'Element'=>array(
'conditions'=>array('ElementsProduct.ElementId = Element.id')
)
)
));
$data = $this->DeliveryNote->find('all',array(
'conditions'=>array(
'Element.id'=>4,
'DeliveryNote.DeliveryDate BETWEEN ? AND ?'=>array('2011-04-22','2011-05-22')),
'fields'=>array('DeliveryNote.DeliveryDate','SUM(DeliveryNotesProduct.Count * ElementsProduct.Count) AS count'),
'group'=>array('DeliveryNote.DeliveryDate')
));
Cakephp bindModel
用于连接表,hasOne
是关联类型之一
http://book.cakephp.org/view/1040/Relationship-Types