dongzi9196 2016-10-04 06:51
浏览 29
已采纳

如何使用cakephp 3.2 inbuilt sum功能在关联表中的sum操作(求和字段)不在父表中?

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

  • 写回答

1条回答 默认 最新

  • dongqiu5184 2016-10-04 15:25
    关注

    You can use the virtual field of the child table, which in your case is the Collections table.

    You can check the documentation to create virtual fields in cakephp3 here

    And in virtual field you can specify the sum function. To write the sum function you can refer here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。