我想以这种方式保存数据:'订单' - >'OrdersFoods' - >'OrdersFoodsChanges'。 OrdersFoodsTable是一个连接表(订单 - 食品)。
为什么我要这样做? Table OrdersFoods包含有关订购食品(ID)的信息,但我想以特定顺序保存有关该食品所用每种成分的其他数据。这就是为什么我从表OrdersFoods创建了具有id的附加表(OrdersFoodsChanges)。
我可以将数据保存到连接表,但不能保存到另一个表(OrdersFoodsChangesTable)。
我尝试过最简单的方法:
$order = $this->Orders->patchEntity($order, $this->request->data, [
'associated' => [
'Foods._joinData.OrdersFoodsChanges'
//'Foods.OrdersFoods.OrdersFoodsChanges'
]
但没有运气。
patchEntity对象
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'
}
正如我从sql log中看到的那样,Cake甚至没有尝试将任何东西保存到最后一个表中。
如果有任何帮助,我将不胜感激。
答案 0 :(得分:0)
所以主要问题是默认情况下,蛋糕保存方法只能进行一级关联。
默认情况下,save()方法还会保存一个关联级别
当我添加相关内容时,一切顺利。
if ($this->Orders->save($order, [
'associated' => ['Foods._joinData.OrdersFoodsChanges']
]))