如何在cakephp中执行此查询条件?

时间:2011-11-28 20:57:37

标签: sql cakephp join

如何以CakePHP的方式进行此查询?

SELECT *
FROM uploaded_sales us, sales s
WHERE us.item_id = s.audience_id

item_id表的uploaded_sales列不是其主键。

audience_id表的sales列也不是它的主键。

我在我的模型上尝试了这个,我没有收到任何错误,但仍然将sales作为空白返回:

$reports = $this->find('all',
  array(
    'joins' => array(
      array(
        'table' => 'sales',
        'alias' => 'Sale',
        'type' => 'left',
        'conditions' => array('Sale.audience_id' => 'UploadedSale.item_id')
    )),
    'conditions' => array(
      'UploadedSale.month' => $month,
      'UploadedSale.year' => $year,
      'UploadedSale.company_id' => $company_id,
      'UploadedSale.item_type' => $item_type
    ),
    'fields' => $fields
));

return $reports;

1 个答案:

答案 0 :(得分:1)

了解如何在CakePHP中使用Containable行为:

http://book.cakephp.org/view/1323/Containable

它将使连接变得更容易。