【问题标题】:Cakephp 3 hasMany association array to convert in proper json formatCakephp 3 hasMany关联数组以正确的json格式转换
【发布时间】:2018-10-18 17:13:23
【问题描述】:

我有一个数组如下,我需要以json格式排列。我正在使用 cakephp3,其中员工表与里程和费用表有很多关系。我检索了我需要以正确的 json 格式排列的完整数组。我试过但无法转换为正确的 json 格式。

Array
(
    [0] => App\Model\Entity\Employee Object
        (
            [id] => 246
            [department] => Management
            [employee_name] => Employee_1
            [car_registration] => uk074wzx
            [expenses] => Array
            (
                [0] => App\Model\Entity\Expense Object
                    (
                        [employee_id] => 246
                        [expense_amount] => 160
                    )

                [1] => App\Model\Entity\Expense Object
                    (
                        [employee_id] => 246
                        [expense_amount] => 150              
                    )
            )

            [mileages] => Array
                (
                    [0] => App\Model\Entity\Mileage Object
                    (
                        [employee_id] => 246
                        [mileage_km] => 8.918
                        [mileage_amount] => 78.4
                        [subsistence] => domestic_day
                        [subsistence_rate] => 33.61
                    )

                    [1] => App\Model\Entity\Mileage Object
                    (
                        [employee_id] => 246
                        [mileage_km] => 70.671
                        [mileage_amount] => 78.4
                        [day_allowance] => 33.61
                    )

                    [2] => App\Model\Entity\Mileage Object
                    (
                        [employee_id] => 246
                        [mileage_km] => 40.997
                        [mileage_amount] => 78.4
                        [day_allowance] => 33.61
                    )
            )
    )

    [1] => App\Model\Entity\Employee Object
    (
        [id] => 247
        [department] => Testing
        [employee_name] => Employee_2
        [car_registration] => uk074wzx
        [expenses] => Array
            (
                [0] => App\Model\Entity\Expense Object
                    (
                        [employee_id] => 247
                        [expense_amount] => 50
                    )

                [1] => App\Model\Entity\Expense Object
                    (
                        [employee_id] => 247
                        [expense_amount] => 30
                    )
            )

        [mileages] => Array
            (
                [0] => App\Model\Entity\Mileage Object
                    (
                        [employee_id] => 247
                        [mileage_km] => 131.137
                        [mileage_amount] => 78.4
                        [day_allowance] => 33.61
                    )

                [1] => App\Model\Entity\Mileage Object
                    (
                        [employee_id] => 247
                        [mileage_km] => 131.018
                        [mileage_amount] => 78.4
                        [day_allowance] => 33.61
                    )
            )
    )

    [2] => App\Model\Entity\Employee Object
    (
        [id] => 248
        [department] => Design
        [employee_name] => sbn emp3
        [car_registration] => uk074wzx
        [expenses] => Array
            (
            )

        [mileages] => Array
            (
            )
    )
)

我需要安排的Json格式:

"data": [
    {
    "date": "2018-04-30(month end date)",
    "items": [
            {
                "employee_id": 246,
                "employee_name": "Employee_1",
                "department": "Management",
                "car_registration": "uk074wzx",
                "mileage_km": 120.586,
                "mileage_amount": 235.2,
                "day_allowance": 100.83,
                "night_allowance": 0
                "expense_amount": 310
            },
            {
                "employee_id": 247,
                "employee_name": "Employee_2",
                "department": "Testing",
                "car_registration": "uk074wzx",
                "mileage_km": 382.741,
                "mileage_amount": 392,
                "day_allowance": 168.05,
                "night_allowance": 0
                "expense_amount": 80
            },
            {
                "employee_id": 248,
                "employee_name": "sbn emp3",
                "department": "Design",
                "car_registration": "uk074wzx",
                "mileage_km": 0,
                "mileage_amount": 0,
                "day_allowance": 0,
                "night_allowance": 0
                "expense_amount": 0
            }
        ]
    }
]

请帮我将数组转换为 json。谢谢。

【问题讨论】:

  • 你的努力在哪里??
  • 也许你正在将结果集转换为只有数组,而不是尝试这个 "$result->enableHydration(false)->toArray(); 在 CakePHP 3.4.0 之前使用 ->hydrate(假);

标签: php arrays json cakephp-3.0


【解决方案1】:

在 EmployeeController 类中

$employee = $this->Employee->find('all')
                 ->contain(['Expenses', 'Mileages'])
                 ->enableHydration(false)
                 ->toArray();

//prepare json data
$emp = array();
foreach($employee as $key => $value) {
    $emp[] = [
        "employee_id" = $value['employee_id'] ?? '',
        "employee_name" = $value['employee_name'] ?? '',
        "department" = $value['department'] ?? '',
        "car_registration" = $value['car_registration'] ?? ''
     ];
     if(!empty($value['mileages'])) {
         $mileages= new Collection($value['mileages']);
         $emp[] = [
         "mileage_km " = $mileages->sumOf('mileage_km') ??'',
         "mileage_amount" = $mileages->sumOf('mileage_amount') ?? '',
         "day_allowance" = $mileages->sumOf('day_allowance') ?? '',
         "night_allowance" = $mileages->sumOf('night_allowance') ?? '',
         ];
     }
     if(!empty($value['expenses'])) {
         $expenses= new Collection($value['expenses']);
         $emp[] = [
         "expense_amount" = $expenses->sumOf('expense_amount') ??'',
        ];
     }
}

$this->set([
        'items' => $emp,
        'date': '2018-04-30(month end date)',
        '_serialize' => ['date', 'items']
]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多