使用cakephp按日期分组

quhf5bfb  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(326)

我有一张table Commande 其中包含 prestataire_id , date , heure ,... 所以我想按日期重新组合表格。我试过了

$commande = $this->Commande->find('all', array('conditions' => array('Commande.prestataire_id' => $this->request->data['prestataire_id']),'order' =>    array('Commande.date' => 'DESC')));

json响应是这样的

{
    "status": "Success",
    "status_code": 200,
    "message": "Commande trouvé",
    "data": {
        "get_Mission": {
            "data": [
                {
                    "Commande": {
                        "id": "475",
                        "date": "2018-04-20",
                        "heure": "10:00:00",
                        "date_created": "2018-04-19",
                        "heure_created": "17:11:39",
                        "date_accepted": "2018-04-19",
                        "heure_accepted": "17:12:35",
                        "delai": "00:00:56",
                        "date_fin": "0000-00-00",
                        "heure_fin": "00:00:00",

                    },

                },
                {
                    "Commande": {
                        "id": "476",
                        "date": "2018-04-20",
                        "heure": "10:07:00",
                        "date_created": "2018-04-19",
                        "heure_created": "17:13:12",
                        "date_accepted": "0000-00-00",
                        "heure_accepted": "00:00:00",
                        "delai": "0",
                        "date_fin": "0000-00-00",
                        "heure_fin": "00:00:00",

                    },

                },

                {
                    "Commande": {
                        "id": "477",
                        "date": "2018-04-19",
                        "heure": "17:13:00",
                        "date_created": "2018-04-19",
                        "heure_created": "17:13:48",
                        "date_accepted": "2018-04-19",
                        "heure_accepted": "17:14:23",
                        "delai": "00:00:35",
                        "date_fin": "0000-00-00",
                        "heure_fin": "00:00:00",

                    },

                }
            ]
        }
    }
}

但我希望他们像数据一样按日期重新组合 ["2018-04-20"]-> {commande,commande} , ["2018-04-19"]->{"commande} 有什么建议吗?

zd287kbt

zd287kbt1#

您可以尝试将月份设置为数组键。

foreach($examples as $example){
        $month = date('Y-m-d', strtotime($example['Model']['date']));
        $exampleArray[$month][] = $example;
    }

并在设置后重新使用$examplearray。

相关问题