【问题标题】:Sum values in relation 3 layers in CakePHPCakePHP中关系3层的总和值
【发布时间】:2017-03-07 13:59:16
【问题描述】:

休斯顿,我有问题;

问题

所以,我的数据库中有一个 EAV 模型,实体、属性、值。之间的关系是:

  • Production hasMany Entity -> Entity belongsTo Production
  • Entity hasMany Attribute -> Attribute belongsTo Entity
  • 属性有很多值 -> 值属于属性

表格值 ---值(INT 3), ---标签(VARCHAR 65), ---id (INT 11 AI)

我可以将关系保存在我的数据库中,但是,我的应用程序需要将 Value.value 的 SUM 返回一个特定的生产,例如:

最终用户,需要按创建月份合并为生产。所以,现在,我的申请返回是这样的:

(int) 0 => array(
        'Production' => array(
            'myconditions' => 'HereIsNotAProblem'
        ),
        'Entity' => array(
            (int) 0 => array(
                'id' => '11',
                'label' => 'Acompanhamento Familiar',
                'production_id' => '8',
                    'Attribute' => array(
                        (int) 0 => array(
                            'id' => '33',
                            'label' => 'Nascidos vivos no m?s',
                            'entity_id' => '11',
                            'Value' => array(
                                (int) 0 => array(
                                    'id' => '1',
                                    'label' => '< 2500g',
                                    'value' => '0',
                                    'attribute_id' => '33'
                                    ),              
                            )
                    )
            )
)

我需要返回:

(int) 0 => array(
        'Production' => array(
            'myconditions' => 'HereIsNotAProblem'
        ),
        'Entity' => array(
            (int) 0 => array(
                'id' => '11',
                'label' => 'Acompanhamento Familiar',
                'production_id' => '8',
                    'Attribute' => array(
                        (int) 0 => array(
                            'id' => '33',
                            'label' => 'Nascidos vivos no m?s',
                            'entity_id' => '11',
                            'Value' => array(
                                (int) 0 => array(
                                    'id' => '1',
                                    'label' => '< 2500g',
                                    'SUM' => 'TOTAL_SUM',
                                    'attribute_id' => '33'
                                    ),              
                            )
                    )
            )
)

我该怎么做?

【问题讨论】:

    标签: mysql entity-framework cakephp


    【解决方案1】:

    因此,当您查询数据时,您需要将值左连接到属性并使用 SUM 表达式。您可以在此处看到类似的示例,但使用 COUNT:https://book.cakephp.org/3.0/en/orm/query-builder.html#using-leftjoinwith

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多