使用 JOIN 的 CakePHP 2 查找方法问题
Issue with CakePHP 2 find method with JOIN
I am facing an issue with CakePHP Find method with JOIN and bindModel.
I have 2 tables, Customer and Order.
Order table has all order records for Customer.
现在在一个查询中,我需要 Customer record
以及 Total
到期或总订购金额。
当我尝试使用这个查询时,它returns只有一条记录。
$this->Customer->bindModel(['hasOne'=>['Order'=>['fields'=>['SUM(Order.due_amount) as due']]]],false);
$data = $this->Customer->find('all',[ 'conditions' => [ 'Customer.type' => 2,'Customer.status'=>1] ]);
2,当我尝试使用 JOIN 时,仍然没有得到正确的结果
$data = $this->Customer->find('all',[
'conditions' => [ 'Customer.type' => 2,'Customer.status'=>1],
'joins' =>[[ 'table' => 'orders', 'alias' => 'Order',
'type' => 'RIGHT', /*LEFT/INNER*/
'fields'=>['SUM(Order.due_amount) as due'],
'conditions' => [ 'Order.customer_id = Customer.id']]]]);
使用这两种方法我都没有得到正确的结果
我需要这样的结果
Array(
[0] => Array ( [Customer] => Array ( /*all Customer table fields */ ) [Order] => Array ( [due] => 125.25))
[1] => Array ( [Customer] => Array ( /*all Customer table fields */ ) [Order] => Array ( [due] => 10.00))
[2] => Array ( [Customer] => Array ( /*all Customer table fields */ ) [Order] => Array ( [due] => 500.10))
.... so on
)
您可以使用以下查询
$joins = array(
array('table' => 'orders',
'alias' => 'Order',
'type' => 'INNER',
'conditions' => array(
'Order.customer_id = Customer.id',
)
)
);
$conditions = [];
$conditions[] = ['Customer.type' => 2];
$conditions[] = ['Customer.status'=>1];
$this->Customer->virtualFields['total_due'] = "sum(Order.due_amount)";
$data_array = $this->Customer->find('all', ['conditions' => $conditions, 'group' => 'Customer.id', 'joins' => $joins, 'fields' => ['Customer.*', 'Customer.total_due']]);
I am facing an issue with CakePHP Find method with JOIN and bindModel.
I have 2 tables, Customer and Order. Order table has all order records for Customer.
现在在一个查询中,我需要 Customer record
以及 Total
到期或总订购金额。
当我尝试使用这个查询时,它returns只有一条记录。
$this->Customer->bindModel(['hasOne'=>['Order'=>['fields'=>['SUM(Order.due_amount) as due']]]],false);
$data = $this->Customer->find('all',[ 'conditions' => [ 'Customer.type' => 2,'Customer.status'=>1] ]);
2,当我尝试使用 JOIN 时,仍然没有得到正确的结果
$data = $this->Customer->find('all',[
'conditions' => [ 'Customer.type' => 2,'Customer.status'=>1],
'joins' =>[[ 'table' => 'orders', 'alias' => 'Order',
'type' => 'RIGHT', /*LEFT/INNER*/
'fields'=>['SUM(Order.due_amount) as due'],
'conditions' => [ 'Order.customer_id = Customer.id']]]]);
使用这两种方法我都没有得到正确的结果
我需要这样的结果
Array(
[0] => Array ( [Customer] => Array ( /*all Customer table fields */ ) [Order] => Array ( [due] => 125.25))
[1] => Array ( [Customer] => Array ( /*all Customer table fields */ ) [Order] => Array ( [due] => 10.00))
[2] => Array ( [Customer] => Array ( /*all Customer table fields */ ) [Order] => Array ( [due] => 500.10))
.... so on
)
您可以使用以下查询
$joins = array(
array('table' => 'orders',
'alias' => 'Order',
'type' => 'INNER',
'conditions' => array(
'Order.customer_id = Customer.id',
)
)
);
$conditions = [];
$conditions[] = ['Customer.type' => 2];
$conditions[] = ['Customer.status'=>1];
$this->Customer->virtualFields['total_due'] = "sum(Order.due_amount)";
$data_array = $this->Customer->find('all', ['conditions' => $conditions, 'group' => 'Customer.id', 'joins' => $joins, 'fields' => ['Customer.*', 'Customer.total_due']]);