0
votes

I would like to save data this way: 'Orders'->'OrdersFoods'->'OrdersFoodsChanges'. Where OrdersFoodsTable is a join table (Orders - Foods).

Why do I want to do so? Table OrdersFoods contains information about ordered foods (ids), but I would like to save additional data about every ingredient used for that food in particular order. That's why I've created additional table (OrdersFoodsChanges) with id from table OrdersFoods.

I can save data to a joined table but not to another table (OrdersFoodsChangesTable).

I've tried to do it the simplest way:

            $order = $this->Orders->patchEntity($order, $this->request->data, [
            'associated' => [
                'Foods._joinData.OrdersFoodsChanges'
                //'Foods.OrdersFoods.OrdersFoodsChanges'
            ]

but with no luck.

patchEntity object

object(Admin\Model\Entity\Order) {

'user_id' => (int) 1,
'city' => '',
'postal_code' => '',
'street' => '',
'house' => '',
'phone' => '',
'foods' => [
    (int) 0 => object(Admin\Model\Entity\Food) {

        'id' => (int) 15,
        'name' => 'Royal',
        'price' => (float) 15,
        'category_id' => (int) 2,
        'photo_id' => (int) 69,
        'shop_id' => (int) 1,
        'favorite' => false,
        'vat' => (int) 23,
        'visible' => true,
        'position' => (int) 0,
        '_joinData' => object(Admin\Model\Entity\OrdersFood) {

            'quantity' => (int) 1,
            'orders_foods_changes' => [
                (int) 0 => object(Admin\Model\Entity\OrdersFoodsChange) {

                    'component_quantity' => (int) 25,
                    'component_id' => (int) 1,
                    'order_food_id' => (int) 1,
                    'type' => 'ADD',
                    'element_num' => (int) 1,
                    '[new]' => true,
                    '[accessible]' => [
                        '*' => true
                    ],
                    '[dirty]' => [
                        'component_quantity' => true,
                        'component_id' => true,
                        'order_food_id' => true,
                        'type' => true,
                        'element_num' => true
                    ],
                    '[original]' => [],
                    '[virtual]' => [],
                    '[errors]' => [],
                    '[invalid]' => [],
                    '[repository]' => 'Admin.OrdersFoodsChanges'

                }
            ],
            '[new]' => true,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [
                'quantity' => true,
                'orders_foods_changes' => true
            ],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'Admin.OrdersFoods'

        },
        '[new]' => false,
        '[accessible]' => [
            '*' => true
        ],
        '[dirty]' => [
            '_joinData' => true
        ],
        '[original]' => [
            '_joinData' => [
                'quantity' => '1',
                'orders_foods_changes' => [
                    (int) 0 => [
                        'component_quantity' => (int) 25,
                        'component_id' => (int) 1,
                        'order_food_id' => (int) 1,
                        'type' => 'ADD',
                        'element_num' => (int) 1,
                        'id' => (int) 2
                    ]
                ]
            ]
        ],
        '[virtual]' => [],
        '[errors]' => [],
        '[invalid]' => [],
        '[repository]' => 'Admin.Foods'

    }
],
'price' => (float) 15,
'shop_id' => (int) 1,
'[new]' => true,
'[accessible]' => [
    '*' => true
],
'[dirty]' => [
    'user_id' => true,
    'type' => true,
    'city' => true,
    'postal_code' => true,
    'street' => true,
    'house' => true,
    'phone' => true,
    'foods' => true,
    'price' => true,
    'shop_id' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Admin.Orders'

}

As I can see from sql log Cake does not even tries to save anything to a last table.

I would be grateful for any help.

1
What is the actual issue? What doesn't work? Error message? Model examples? - Aleksander Wons
@awons No error while saving. I've just edited my post. My guess is that something is wrong with OrdersFoodsChanges entity. Some stupid mistake. I've added "fields" manualy in cotroller ex.: $this->request->data['foods'][0]['_joinData']['orders_foods_changes'][0]['component_id'] = 1; - Szymon Fabianski

1 Answers

0
votes

So the main problem was that by default cake save method makes only one level association.

By default the save() method will also save one level of associations

When I've added associated it all went well.

        if ($this->Orders->save($order, [
            'associated' => ['Foods._joinData.OrdersFoodsChanges']
        ]))