0

I need to create the upload process where the user can upload multiple files at once, using the file field with html5 multiple attr. Name of the file must be saved in the associated model.

I can run successfully upload one file and save the file name in the photos table, across the field:

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

But if I want to enable upload more photos with

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

I get into the problem, and try to understand where I make mistakes, but without success.

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

All standard callbacks where I currently perform debug $data / $entity, but only in beforeMarshal i use:

$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
],
...

In beforeSave and afterSave

My form is OK, the data properly come in before Marshal method, if I upload 3 files, I also see the same number of debug outputs, but in beforSave and afterSave debug only show the first file like this:

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'

}

Edit:

In the purpose of the test, I create such a form:

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']);

Also it only be saved the first result.

I need help to understand how to save multiple form data. Where I go wrong?

5
  • You do not show any of your actual uploading process, nor do you really mention any specific technical problem. People are going to have a hard time answering your question.
    – ndm
    Commented Oct 7, 2015 at 7:48
  • I added some new details. The same thing happens when I want to save multiple name only, please look at the last example of the form. Upload and processing of files is not the subject of questions, at this time trying to save the only name in the relational table.
    – Salines
    Commented Oct 7, 2015 at 7:58
  • 1
    Uploading/Processing might not be subject to the question, but it might be important to know what this unknown Upload behavior does. If not even your new example, which doesn't involve any uploads, is not saving properly, then the behavior might be involed in this. Maybe you should remove all this noise from your question and make it about what matters, that is, 1.) How exactly do you need the posted data (all data) to be structured for a successful save? 2.) In what structure exactly does the posted data (all data) currently arrive? Given that a successfull save is possible at all.
    – ndm
    Commented Oct 7, 2015 at 8:52
  • You are right. I detach 'unknown' upload behavior, and can now save multiple data into a relation table (sent from the last example of the form). I'll have to create a new question, how the behavior handle multiple data. Thank you.
    – Salines
    Commented Oct 7, 2015 at 12:28
  • Thank you for your reply, my fault, I put in afterSave exit method, and stop storing multiple data. I have the habit to put die or exit after a debug function. I did not know the model and behavior repeatedly used when storing multiple data.
    – Salines
    Commented Oct 7, 2015 at 18:30

1 Answer 1

0

I think your field should be like this

echo $this->Form->file('photos.name.',['multiple'=>true]); //note dot notation at end of name. It will generate input name as array
1
  • No, that approach is applicable in CakePHP 2 version.
    – Salines
    Commented Oct 7, 2015 at 7:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.