2013-10-19 53 views
3

在安装cake 2.3的saveMany()时出现问题。它不返回来自控制器或模型的错误(我也删除了所有验证)。我检查数据正在通过,这是输出:cakePHP saveMany不能正常工作

array(
    'Xfilter' => array(
     'Xfilter0user_id' => '2', 
     'Xfilter0name' => 'Modern Age (1991-present)', 
     'Xfilter0search' => '/listings/find/coverDateBetween:1991 - 2014', 
     'Xfilter0user_tab' => '1', 
     'Xfilter1user_id' => '2', 
     'Xfilter1name' => 'Copper Age (1984-1991)', 
     'Xfilter1search' => '/listings/find/coverDateBetween:1984 - 1991', 
     'Xfilter1user_tab' => '1' 
    ) 
) 

这里是我的控制器:

public function add() { 
     if ($this->request->is('post')) { 
      //debug($this->request->data);   
      //$this->Xfilter->create(); 
      if ($this->Xfilter->saveMany($this->request->data['Xfilter'])) { 
       $this->Session->setFlash(__('The Xfilter has been saved')); 
       $this->redirect($this->referer(true)); 
       echo debug($this->Xfilter->invalidFields()); 
      } else { 
       $this->Session->setFlash(__('The Xfilter could not be saved. Please, try again.')); 
      } 
     } 
     $users = $this->Xfilter->User->find('list'); 
     $this->set(compact('users')); 
    } 

和我的观点:

<?php echo $this->Form->create('Xfilter'); ?> 

    <?php if(!empty($xfilters)){ foreach ($xfilters as $key => $xfilter): ?> 
     <fieldset class="pure-u-1"> 

      <?php 
       echo $this->Form->input('Xfilter'.$key.'user_id', array('value'=>'2','type'=>'hidden'));     
       echo $this->Form->input('Xfilter'.$key.'name', array('value'=>$xfilter['Xfilter']['name'])); 
       echo $this->Form->input('Xfilter'.$key.'search', array('value'=>$xfilter['Xfilter']['search'])); 
       echo $this->Form->input('Xfilter'.$key.'user_tab', array('label' => 'Set as a filter tab?','value'=>'1')); 
      ?>    
     </fieldset> 


    <?php endforeach;} ?> 
    <?php 
     $options = array(
      'label' => __('Submit'), 
      'class' => 'pure-button pure-button-primary' 
     ); 
     echo $this->Form->end($options); 
    ?> 

什么好主意,因为我错过了什么?

回答

4

你的数据格式不正确,它必须是一个数组的数组,无论是在

array(
    array('field1' => 'valuea', 'field2' => 'valuea'), 
    array('field1' => 'valueb', 'field2' => 'valueb'), 
    array('field1' => 'valuec', 'field2' => 'valuec') 
) 

array(
    array('Model' => array('field1' => 'valuea', 'field2' => 'valuea')), 
    array('Model' => array('field1' => 'valueb', 'field2' => 'valueb')), 
    array('Model' => array('field1' => 'valuec', 'field2' => 'valuec')) 
) 

格式。

的第一个样式可以通过表单辅助使用适当的dot notation来实现:

// -----------------------------v -----------v spot the dot 
echo $this->Form->input('Xfilter.' . $key . '.user_id', array(
    'value' => '2', 
    'type' => 'hidden' 
)); 

即字段名应该结束了,就像这样Xfilter.0.user_id

又见

+1

由于NDM,点失踪。 ('value'=>'2','type'=>'hidden'));}} $ echo $ this-> Form-> input('Xfilter。'。$ key。'。user_id',array为所有输入工作 – squeaker