【问题标题】:How create a grouped structure based off of a field in a query result?如何根据查询结果中的字段创建分组结构?
【发布时间】:2016-01-12 16:41:21
【问题描述】:

我正在尝试在 cakephp3 中按年份分组。我能够使用 以下方式,但它仍然没有按年份分组(我想要的方式。)

$query = $this->Alerts->query();
$year = $query->func()->year([
    'added_on' => 'literal'
]);

$month = $query->func()->monthname(['added_on' => 'literal']);
$monthAlertsCount = $query->func()->count($month);
$data = $query
    ->select([
        'year' => $year,
        'month' => $month,
        'count' => $monthAlertsCount
    ])
    ->group($year)
    ->group($month);

$status = "success";
$this->set('response', $status);
$this->set('year', $data);
$this->set('_serialize', ['response','year']);

当前输出如下:

{

    "response":"success",
    "year":[
        {
            "year":"2013",
            "month":"November",
            "count":"1"
        },
        {
            "year":"2014",
            "month":"February",
            "count":"2"
        },
        {
            "year":"2014",
            "month":"January",
            "count":"3"
        },
        {
            "year":"2015",
            "month":"December",
            "count":"6"
        },
        {
            "year":"2015",
            "month":"February",
            "count":"3"
        },
        {
            "year":"2015",
            "month":"January",
            "count":"4"
        },
        {
            "year":"2016",
            "month":"January",
            "count":"83"
        }
    ]

}

预期输出:

{

    "response":"success",
    "year":[
        {
            "2013":[
                {
                    "month":"November",
                    "count":"1"
                }
            ],
            "2014":[
                {
                    "month":"February",
                    "count":"2"
                },
                {
                    "month":"January",
                    "count":"3"
                }
            ],
            "2015":[
                {
                    "month":"December",
                    "count":"6"
                },
                {
                    "month":"February",
                    "count":"3"
                },
                {
                    "month":"January",
                    "count":"4"
                }
            ],
            "2016":[
                {
                    "month":"January",
                    "count":"83"
                }
            ]
        }
    ]

}

谁能帮我在 cakephp3 中得到预期的输出?

【问题讨论】:

    标签: cakephp orm query-builder cakephp-3.1


    【解决方案1】:

    使用集合方法groupBy() - 请记住,查询是集合对象(有点)!

    $data = $query
        ->select([
            'year' => $year,
            'month' => $month,
            'count' => $monthAlertsCount
        ])
        ->group($year)
        ->group($month)
        ->groupBy('year');
    

    这将创建所需的分组结构,但它不会从各个行中删除 year 字段,如果您希望删除它们,请使用映射器,例如

    // ...
    ->groupBy('year')
    ->map(function ($rows) {
        foreach ($rows as &$row) {
            unset($row['year']);
        }
        return $rows;
    });
    

    另见

    【讨论】:

    • 嗨,感谢您的回复,它几乎接近我想要的,但我仍然无法删除 year ->groupBy('year') ->map(function ($row) { unset($row['year']); return $row; });不会从输出中删除年份
    • @AkshayMalhotra 忘记了映射器不会遍历组...检查更新的代码示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 2021-11-08
    • 2012-09-23
    • 2017-03-23
    相关资源
    最近更新 更多