我有两张表product
和product_images
这是我的product_image
表
id
product_id
image
time_created
我想以单一形式保存带有product_image的产品
这是我的product/add
表格
<?= $this->Form->create($product ,['type' => 'file','enctype' => 'multipart/form-data'])?>
...
...
<input type="file" name="ProductImages[][image]" multiple>
...
...
<?= $this->Form->end() ?>
这是add
控制器中的products
方法:
public function add()
{
$product = $this->Products->newEntity();
if ($this->request->is('post')) {
$product = $this->Products->patchEntity($product, $this->request->getData(),[
'associated' => [
'ProductImages',
]
]);
if ($this->Products->save($product)) {
$this->Flash->success(__('The product has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The product could not be saved. Please, try again.'));
}
$productCategories = $this->Products->ProductCategories->find('list', ['limit' => 200]);
$this->set(compact('product', 'productCategories'));
$this->set('_serialize', ['product']);
}
但没有在product_image表中保存任何内容
我尝试创建自定义数组以便于测试,但它只是保存了新产品而没有产品图像
$product = $this->Products->patchEntity($product,[
'title'=>"asxdasxc",
"price"=>555,
"alt"=>55,
"productImages"=>[
['image'=>"sssssssss"]
]
],[
'associated' => [
'ProductImages',
]
]);