2015-10-06 184 views
0

我需要創建上傳過程,用戶可以一次上傳多個文件,使用帶有html5多個attr的文件字段。文件的名稱必須保存在關聯的模型中。CakePHP 3:上傳多個文件並保存相關模型中的文件名?

我可以成功運行上傳一個文件,並在照片表保存的文件名,在田野裏:

echo $this->Form->file('photos.name'); 

但是,如果我想啓用上傳更多的照片與

echo $this->Form->input('title'); // post title 
echo $this->Form->input('maintext'); // post main text, 
... etc 
echo $this->Form->file('photos[].name',['multiple'=>true]); 

我進入問題,並試圖瞭解我犯了什麼錯誤,但沒有成功。

PostsController:

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

     if ($this->Posts->save($post)) { 
      $this->Flash->success(__('The post has been saved.')); 
      return $this->redirect(['action' => 'index']); 
     } else { 
      $this->Flash->error(__('The post could not be saved. Please, try again.')); 
     } 
    } 
    $this->set(compact('post')); 
    $this->set('_serialize', ['post']); 
} 

PostsTable:

$this->addBehavior('Upload'); 

$this->hasMany('Photos', [ 
    'foreignKey' => 'post_id' 
]); 

UploadBehavior

所有標準的回調在我目前進行調試$數據/ $實體,但只有在beforeMarshal我使用:

$data = Hash::get($data,'name'); 
debug($data); 
// debug output 
[ 
'name' => 'HPIM3869.JPG', 
'type' => 'image/jpeg', 
'tmp_name' => 'C:\xampp\tmp\phpF02D.tmp', 
'error' => (int) 0, 
'size' => (int) 1295448 
], 
... 

在beforeSave和afterSave

我的形式是好的,數據正確元帥方法之前進來,如果我上傳3個文件,我也看到了相同數量的調試輸出,但在beforSave和afterSave調試只顯示第一個文件是這樣的:

debug($entity); 

object(App\Model\Entity\Photos) { 

    'name' => [ 
     'name' => 'HPIM3435.JPG', 
     'type' => 'image/jpeg', 
     'tmp_name' => 'C:\xampp\tmp\php5839.tmp', 
     'error' => (int) 0, 
     'size' => (int) 1517410 
    ], 
    'post_id' => (int) 469, 
    'created' => object(Cake\I18n\Time) { 

     'time' => '2015-10-07T09:22:44+0200', 
     'timezone' => 'Europe/Berlin', 
     'fixedNowTime' => false 

    }, 
    'modified' => object(Cake\I18n\Time) { 

     'time' => '2015-10-07T09:22:44+0200', 
     'timezone' => 'Europe/Berlin', 
     'fixedNowTime' => false 

    }, 
    '[new]' => true, 
    '[accessible]' => [ 
     '*' => true 
    ], 
    '[dirty]' => [ 
     'name' => true, 
     'post_id' => true, 
     'created' => true, 
     'modified' => true 
    ], 
    '[original]' => [], 
    '[virtual]' => [], 
    '[errors]' => [], 
    '[repository]' => 'Photos' 

} 

編輯:

在測試的目的,創建這樣的形式:

echo $this->Form->input('name',['value'=>'zzz']); 
echo $this->Form->input('photos.0.name',['value'=>'zzz']); 
echo $this->Form->input('photos.1.name',['value'=>'hhh']); 
echo $this->Form->input('photos.2.name',['value'=>'fff']); 

此外,它僅被保存在第一個結果。

我需要幫助來了解如何保存多個表單數據。我哪裏出錯了?

+0

你不表現出任何實際的上傳過程中,也不是你真的提到任何具體的技術問題。人們將很難回答你的問題。 – ndm

+0

我添加了一些新的細節。同樣的事情發生時,我只想保存多個名稱,請查看錶單的最後一個示例。上傳和處理文件不是問題的主題,此時試圖保存關係表中的唯一名稱。 – Salines

+1

上傳/處理可能不受此問題的影響,但瞭解此未知上傳行爲的功能可能很重要。如果甚至連你的新示例(不涉及任何上傳)都沒有正確保存,那麼這個行爲可能會被記錄下來。也許你應該從你的問題中消除所有這些噪音,並且重點關注什麼,也就是1。)您如何準確構建保存的發佈數據(_all_ data)? 2.)發佈數據(_all_數據)目前到達的結構究竟是什麼?鑑於成功的保存是可能的。 – ndm

回答

0

我覺得你的領域應該是這樣的

echo $this->Form->file('photos.name.',['multiple'=>true]); //note dot notation at end of name. It will generate input name as array 
+0

不,該方法適用於CakePHP 2版本。 – Salines