2016-08-24 41 views
0

我使用CakePHP 3.2和proffer插件上傳圖像。在cakephp中使用proffer進行多圖像上傳

我有productsproduct_images表和使用單個表單上傳數據到兩個表。

這是我的形式是如何

<?= $this->Form->create($product, ['type' => 'file') ?> 
<?= $this->Form->input('title', ['required' => true]) ?> 
<?= $this->Form->input('description', ['required' => true]) ?> 
<?= $this->Form->input('product_images.0.image', ['required' => true, 'type' => 'file']) ?> 
<?= $this->Form->input('product_images.1.image', ['type' => 'file']) ?> 
<?= $this->Form->button('submit', ['type' => 'submit']) ?> 
<?= $this->Form->end() ?> 

和控制器就像是

public function add() 
{ 
    $product = $this->Products->newEntity(); 
    if ($this->request->is('post')) { 
    $product = $this->Products->patchEntity($product, $this->request->data, [ 
     'associated' => ['ProductImages'] 
    ]); 

    if ($this->Products->save($product)) { 
     $this->Flash->success('Product saved'); 
    } 
    } 
} 

但這將數據保存到產品表,而不是產品的圖片。

回答

0

我明白你的模型正確關聯

<?= $this->Form->create($product, ['type' => 'file']) ?> 
<?= $this->Form->input('title', ['required' => true]) ?> 
<?= $this->Form->input('description', ['required' => true]) ?> 
<?= $this->Form->input('product_images.0', ['required' => true, 'type' => 'file']) ?> 
<?= $this->Form->input('product_images.1', ['type' => 'file']) ?> 
<?= $this->Form->button('submit', ['type' => 'submit']) ?> 
<?= $this->Form->end() ?> 

添加名稱圖像數據庫來保存

public function add() 
    { 
     $product = $this->Products->newEntity(); 
     if ($this->request->is('post')) { 
      $product = $this->Products->patchEntity($product, $this->request->data); 

     foreach($product['product_images'] as $image) { 
      //Add image name who you want to save in database 
      $product['product_images'][]['image'] = $image['name'];  
     } 

     if ($this->Products->save($product)) { 
      $this->Flash->success('Product saved'); 
     } 
     } 
    }