0
我想以這種方式保存數據:'Orders' - >'OrdersFoods' - >'OrdersFoodsChanges'。 OrdersFoodsTable是一個連接表(Orders - Foods)。Cakephp 3將數據保存到連接表和與該連接表關聯的表中
爲什麼我要這麼做? Table OrdersFoods包含有關訂購食品(ids)的信息,但我想按特定順序保存有關用於該食品的每種成分的其他數據。這就是爲什麼我使用OrdersFoods表創建的附加表(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日誌蛋糕看到甚至沒有嘗試任何東西保存到最後的表。
我將不勝感激任何幫助。
實際問題是什麼?什麼不行?錯誤信息?模型示例? –
@awons保存時沒有錯誤。我剛剛編輯了我的帖子。我的猜測是OrdersFoodsChanges實體出了問題。一些愚蠢的錯誤。我已經在cotroller ex中添加了「fields」manualy:$ this-> request-> data ['foods'] [0] ['joinData'] ['orders_foods_changes'] [0] ['component_id'] = 1; –