【问题标题】:CakePHP saveMany doesnt saveCakePHP saveMany 不保存
【发布时间】:2015-08-16 09:52:41
【问题描述】:

我有以下表单代码。

<?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;
        }
    }

错误:

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

【问题讨论】:

  • 您的表单数据没有任何内容,它的格式似乎正确。如果发生验证错误并导致 saveMany 返回 false,则可能会检查您的模型验证规则,因为保存操作会暂停

标签: cakephp cakephp-2.5 savemany


【解决方案1】:
 $this->PagePhoto->saveMany($this->request->data['PagePhoto']);

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多