[英]Insert to multiple tables in CakePHP
我是CakePHP的新手。 現在,我正在努力解決過去一個小時在互聯網上找不到答案的問題。 我正在嘗試將以下數組保存到數據庫中:
array(
'Product' => array(
'product_id' => '77adfe7754esda71r1999431',
'supplier_id' => 'zalando_it',
'product_name' => 'Sweater',
'price_new' => '68.97',
'affiliate_url' => 'http://example.com'
),
'Image' => array(
'image' => '99988_DP.jpg'
)
)
現在,這是我的ERD的一個示例: https : //www.dropbox.com/s/1zvfqfxes53gibb/example.png
我在模型中正確連接了表(通過使用belongsTo和hasMany變量),但是我仍然認為我做錯了...。原因是在以下情況下使用函數$ this-> Supplier-> saveAssociated($ data)模型“ Feed”不會保存任何內容。 當我使用$ this-> Supplier-> Product-> saveAssociated($ data)時,它將保存Product並使用product_id在Image表中創建記錄,但將“ image”字段留空。
只有當我使用$ this-> Supplier-> Product-> Image-> saveAssociated($ data)時,它才能正確保存所有內容。 但這不是錯嗎? 在我看來,遍歷每個模型($ this->供應商-> Product-> Image ...)來保存所有表似乎不正確。...還是我錯了?
假設產品有很多圖片,則您的數據需要是這樣的(注意“圖片”已編號,因此可以有多個):
array(
'Product' => array(
'product_id' => '77adfe7754esda71r1999431',
'supplier_id' => 'zalando_it',
'product_name' => 'Sweater',
'price_new' => '68.97',
'affiliate_url' => 'http://example.com'
),
'Image' => array(
0 => array(
'image' => '99988_DP.jpg'
)
)
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.