0

我有以下表格代码。

<?php echo $this
   ->Form
   ->create(
       'PagePhoto',
       array(
           'type' => 'file',
           'url' => array(
                'controller' => 'page_photos',
                'action' => 'add'
            )
       )
  ); ?>
  <div class="modal-body has-padding">
       <div class="form-group">
       <?php echo $this->Form->label('PagePhoto.0.filename', 'Photos:'); ?>
       <br/><br/>
       <?php echo $this->Form->file('PagePhoto.0.filename', array('required' => false)); ?>
       <?php echo $this->Form->error('PagePhoto.0.filename', null, array('class' => 'label label-block label-danger text-left', 'wrap' => 'label')); ?>
       <br/>
       <?php echo $this->Form->file('PagePhoto.1.filename', array('required' => false)); ?>
       <?php echo $this->Form->error('PagePhoto.1.filename', null, array('class' => 'label label-block label-danger text-left', 'wrap' => 'label')); ?>
       <br/>
       <?php echo $this->Form->file('PagePhoto.2.filename', array('required' => false)); ?>
       <?php echo $this->Form->error('PagePhoto.2.filename', null, array('class' => 'label label-block label-danger text-left', 'wrap' => 'label')); ?>
       <br/>
       <?php echo $this->Form->file('PagePhoto.3.filename', array('required' => false)); ?>
       <?php echo $this->Form->error('PagePhoto.3.filename', null, array('class' => 'label label-block label-danger text-left', 'wrap' => 'label')); ?>
    </div>
  </div>
<div class="modal-footer">
     <button type="button" class="btn btn-warning" data-dismiss="modal">Close</button>
     <button type="submit" class="btn btn-primary">Upload Photos</button>
</div>

它将以下数组返回到add操作

array(
    'PagePhoto' => array(
        (int) 0 => array(
            'filename' => array(
                'name' => 'IMG_1683.jpg',
                'type' => 'image/jpeg',
                'tmp_name' => '/Applications/MAMP/tmp/php/phpoIrbZ6',
                'error' => (int) 0,
                'size' => (int) 94131
            )
        ),
        (int) 1 => array(
            'filename' => array(
                'name' => 'IMG_1683.jpg',
                'type' => 'image/jpeg',
                'tmp_name' => '/Applications/MAMP/tmp/php/phpAcbXbC',
                'error' => (int) 0,
                'size' => (int) 94131
            )
        ),
        (int) 2 => array(
            'filename' => array(
                'name' => 'IMG_1683.jpg',
                'type' => 'image/jpeg',
                'tmp_name' => '/Applications/MAMP/tmp/php/phppCeN8G',
                'error' => (int) 0,
                'size' => (int) 94131
            )
        ),
        (int) 3 => array(
            'filename' => array(
                'name' => 'IMG_1683.jpg',
                'type' => 'image/jpeg',
                'tmp_name' => '/Applications/MAMP/tmp/php/php8Ib3bO',
                'error' => (int) 0,
                'size' => (int) 94131
            )
        )
    )
)

添加操作

public function admin_add() {

        if ($this->request->is('post')) {
            debug($this->request->data);
            $this->PagePhoto->create();
            if ($this->PagePhoto->saveMany($this->request->data)) {
                $this->Session->setFlash(__('The gallery has been saved.'), 'admin/flash_success');
                // return $this->redirect(array('action' => 'view', $this->request->data['Gallery']['album_id']));
            } else {
                $this->Session->setFlash(__('The gallery could not be saved. Please, try again.'), 'admin/flash_error');
            }
        } else {
            // $this->request->data['Gallery']['album_id'] = $album_id;
        }
    }

错误:

FAILURE:无法保存图库。请再试一次。

4

1 回答 1

0
 $this->PagePhoto->saveMany($this->request->data['PagePhoto']);

注意:当保存同一模型的多条记录时,记录数组应该只是数字索引而不需要模型键。

于 2015-08-16T12:07:04.643 回答