如何使用 cakephp 3.2 内置求和功能在关联 table 中而不是在 parent table 中进行求和操作(对字段求和)?
How to do sum operation(sum a field) in associate table not in the parent table using cakephp 3.2 inbuilt sum functionality?
我想总结cakephp 3.2中的一个字段。
但是该字段存在于 hasOne 模型关联中。
我想在绑定查询中完成。
我有一个订单 table,我正在使用 collection table 进行模型绑定。
这里我想总结一下collection中的所有due_amount table
我已经试过了,但是没用。
请检查我的代码,有什么问题吗?
$this->Orders->hasOne('Collections', [
'className' => 'Collections',
'foreignKey' => 'order_id',
'strategy' => 'select',
'conditions' => function (\Cake\Database\Expression\QueryExpression $exp, \Cake\ORM\Query $query) {
$query->order(['Collections.id' => 'ASC']);
return [];
}
]);
$get_total_sales = $this->Orders->find('all')->where($condition)->select(['id', 'region_id', 'net_total'])->contain(['Collections' => ['queryBuilder' => function ($q) {
return $q->select(['id', 'order_id', 'total_sale_amount', 'due_amount']);
}]])->order(['Orders.due_date DESC']);
$res2 = $get_total_sales->select(['total_due' =>$get_total_sales->func()->sum('collection.due_amount')])->first();
echo $due = $res2->total_due;//its showing column not find error.
Is it working in order table instead of collections table?
How can i do it for collection table using the $get_total_sales listing results?
Below some out put
[
{
"id": 40,
"region_id": 2,
"net_total": 2899.12,
"collection": {
"id": 182,
"order_id": 40,
"total_sale_amount": 2899.12,
"due_amount": 1990
},
},
{
"id": 38,
"region_id": 2,
"net_total": 110,
"collection": {
"id": 181,
"order_id": 38,
"total_sale_amount": 110,
"due_amount": 10
},
},
{
"id": 39,
"region_id": 2,
"net_total": 16670,
"collection": {
"id": 190,
"order_id": 39,
"total_sale_amount": 16670,
"due_amount": 16630.99
},
},
这里我想总结一下collection中的所有due_amount table
谢谢
我想总结cakephp 3.2中的一个字段。 但是该字段存在于 hasOne 模型关联中。
我想在绑定查询中完成。
我有一个订单 table,我正在使用 collection table 进行模型绑定。
这里我想总结一下collection中的所有due_amount table
我已经试过了,但是没用。
请检查我的代码,有什么问题吗?
$this->Orders->hasOne('Collections', [
'className' => 'Collections',
'foreignKey' => 'order_id',
'strategy' => 'select',
'conditions' => function (\Cake\Database\Expression\QueryExpression $exp, \Cake\ORM\Query $query) {
$query->order(['Collections.id' => 'ASC']);
return [];
}
]);
$get_total_sales = $this->Orders->find('all')->where($condition)->select(['id', 'region_id', 'net_total'])->contain(['Collections' => ['queryBuilder' => function ($q) {
return $q->select(['id', 'order_id', 'total_sale_amount', 'due_amount']);
}]])->order(['Orders.due_date DESC']);
$res2 = $get_total_sales->select(['total_due' =>$get_total_sales->func()->sum('collection.due_amount')])->first();
echo $due = $res2->total_due;//its showing column not find error.
Is it working in order table instead of collections table?
How can i do it for collection table using the $get_total_sales listing results?
Below some out put
[
{
"id": 40,
"region_id": 2,
"net_total": 2899.12,
"collection": {
"id": 182,
"order_id": 40,
"total_sale_amount": 2899.12,
"due_amount": 1990
},
},
{
"id": 38,
"region_id": 2,
"net_total": 110,
"collection": {
"id": 181,
"order_id": 38,
"total_sale_amount": 110,
"due_amount": 10
},
},
{
"id": 39,
"region_id": 2,
"net_total": 16670,
"collection": {
"id": 190,
"order_id": 39,
"total_sale_amount": 16670,
"due_amount": 16630.99
},
},
这里我想总结一下collection中的所有due_amount table
谢谢