cakephp保存关系数据

时间:2017-11-25 13:46:27

标签: cakephp cakephp-3.0

我有两张表productproduct_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',
                ]
            ]);

0 个答案:

没有答案
相关问题