cakePHP saveMany 不工作

Posted

技术标签:

【中文标题】cakePHP saveMany 不工作【英文标题】:cakePHP saveMany not working 【发布时间】:2013-10-19 21:25:29 【问题描述】:

在安装 cake 2.3 时遇到 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);
    ?>

关于我缺少什么的任何好主意?

【问题讨论】:

【参考方案1】:

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

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 之类的形式结束。

另见

http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-savemany-array-data-null-array-options-array http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#field-naming-conventions

【讨论】:

感谢 ndm,点不见了。 echo $this->Form->input('Xfilter.'.$key.'.user_id', array('value'=>'2','type'=>'hidden'));对于所有输入作品

以上是关于cakePHP saveMany 不工作的主要内容,如果未能解决你的问题,请参考以下文章

使用 CakePHP 的 saveMany() 保存 >10000 条记录

CakePHP - 当某些字段为空时,防止模型数据保存在 saveMany 调用中

cakephp saveMany 使用 $fieldList (无表单)

cakephp 3如何增加会话超时

saveMany 验证无法正常工作

在AWS S3上存储CakePHP库以用于多个应用程序