2016-08-24 1 views
0

Я использую CakePHP 3.2 и плагин proffer для загрузки изображений.Загрузка нескольких изображений с использованием proffer в cakephp

У меня есть products и product_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'); 
     } 
     } 
    } 
Смежные вопросы