如何使用cakephp 3.2对模型关联表中的求和运算(求和字段) [英] How to do sum operation(sum a field) in model associate table using cakephp 3.2

查看:95
本文介绍了如何使用cakephp 3.2对模型关联表中的求和运算(求和字段)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 cakephp  3 中总结一个字段 2 。但是字段 中有 hasOne模型关联。 



我想 绑定查询。

我有一个订单表,我正在使用集合表进行模型绑定。

这里我想总结一下
集合表中所有due_amount



我试过但它不起作用。

请检查我的代码,它是否有任何问题。?

$ this-> Orders-> hasOne(' Collections' ,[
' className' = > ' 集合'
' foreignKey' = > ' order_id'
' 策略' = > ' select'
' conditions' = > 函数(\ Cake \Database \Expression \QueryExpression $ exp,\ C ake \ORM\Query $ query){
$ query-> order([' 集合。 id' = > ' ASC' ]);
return [];
}
]);
$ get_total_sales = $ this-> Orders-> find(' all') - > where($ condition) - > select([' id'' region_id'' net_total']) - > contains([' Collections' = < span class =code-keyword>>
[' queryBuilder' = > 函数($ q){
return $ q-> select([' id'' order_id'' total_sale_amount'' due_amount']);
}]]) - >订单([' Orders.due_date DESC']) ;


$ res2 = $ get_total_sales-> select([' total_due' => $ get_total_sales-> func() - > sum(' collection.due_amount'< /跨度>)]) - >首先();

echo $ due = $ res2-> total_due; // 其显示列未找到错误。
订单表中是否正在使用而不是集合表?
我怎样才能 集合表使用 $ get_total_sales列表结果?



低于某些 out

[
{
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
},

},

这里我想总结所有
集合表中的due_amount

谢谢





我尝试过:



$ res2 = $ get_total_sales-> select(['total_due'=> $ get_total_sales- > func() - > sum('collection.due_amount')]) - > first();

解决方案

this-> Orders-> hasOne(' Collections',[
' className' = > ' 集合'
' < span class =code-string> foreignKey' = > ' order_id'
' strategy' = > ' select'
' 条件' = > function(\Cake\Database\Expression\QueryExpression


exp,\ Cake\ORM\Query


query){

I want to sum up a field in cakephp 3.2. But the field is present in hasOne model association.



I want to do it in the bind query .

I have a order table and i am doing model bind with collection table .

Here i want to sum up all the due_amount in collections table

 

I have tried but its not working.

Please check my code,Is there any thing wrong with it.?

 $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
        },

    },

Here i want to sum up all the due_amount in collections table

Thank you



What I have tried:

$res2 = $get_total_sales->select(['total_due' =>$get_total_sales->func()->sum('collection.due_amount')])->first();

解决方案

this->Orders->hasOne('Collections', [ 'className' => 'Collections', 'foreignKey' => 'order_id', 'strategy' => 'select', 'conditions' => function (\Cake\Database\Expression\QueryExpression


exp, \Cake\ORM\Query


query) {


这篇关于如何使用cakephp 3.2对模型关联表中的求和运算(求和字段)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆