Cakephp 3中的Resultset映射器或格式化程序

时间:2017-04-20 10:02:02

标签: cakephp cakephp-3.0

我有2个集合/结果集,第一个是products,第二个是sizes

"products": [
                            {
                                "category_id": 5,
                                "id": 5,
                                "code": "A",
                                "name": "Pizzabrot",
                                "description": "",
                                "product_prices": [
                                    {
                                        "product_id": 5,
                                        "price": 2.5,
                                        "size_id": 15
                                    },
                                    {
                                        "product_id": 5,
                                        "price": 3.5,
                                        "size_id": 16
                                    }
                                ]
                            },
                            {
                                "category_id": 5,
                                "id": 6,
                                "code": "B",
                                "name": "Pizzabrot mit Knoblauch",
                                "description": "",
                                "product_prices": [
                                    {
                                        "product_id": 6,
                                        "price": 3,
                                        "size_id": 15
                                    },
                                    {
                                        "product_id": 6,
                                        "price": 4,
                                        "size_id": 16
                                    }
                                ]
                            }]

"sizes": [
                        {
                            "id": 15,
                            "name": "Klein",
                            "category_id": 5
                        },
                        {
                            "id": 16,
                            "name": "Gro\u00df",
                            "category_id": 5
                        }
                    ]

我想用product_prices.size_id收藏中的每个sizes取代$joining_date = "1976-14-02"; $timeToAdd = "+ 58 years"; $objDateTime = DateTime::createFromFormat("Y-m-d",$joining_date); $objDateTime->modify($timeToAdd); echo "My Retire Date is ".$objDateTime->format("Y-m-d")."<br />"; $retire_date = date('Y-m-d', strtotime($joining_date.$timeToAdd)); echo $retire_date; die;

1 个答案:

答案 0 :(得分:0)

从我所看到的情况来看,最好将ProductPricesSizes关联起来,并使用Products获取它们。

如果出于某种原因这不符合您的需求,您可以使用sizes(请参阅Docs)找到find('list'),如下所示:

$query = $this->Sizes->find('list', [
    'keyField' => 'id',
    'valueField' => 'name'
]);
$sizes = $query->toArray();

然后循环遍历products及其product_prices并执行类似的操作。

$product_price->size_id = $sizes[$product_price->size_id];

请选择第一个解决方案。 ; - )