Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakePHP saveMany not working

Having problems with saveMany() on a cake 2.3 install. It is returning no errors from either the controller or Model (I've removed all validation as well). I've checked that data is being passed and this is the output:

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

And here is my controller:

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

and my view:

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

Any bright ideas as to what I'm missing?

like image 896
squeaker Avatar asked Oct 28 '25 05:10

squeaker


1 Answers

Your data is not formatted correctly, It has to be an array of arrays, either in

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

or in

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

format.

The first style can be achieved by using proper dot notation in the form helper:

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

ie the field name should end up as something like Xfilter.0.user_id.

See also

  • 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
like image 143
ndm Avatar answered Oct 31 '25 08:10

ndm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!