2

I have a form that has multiple entries to the same table, the problem I'm having is that it only sends the last entry to the database and doesn't send the rest. I have used the debug and it only prints out the last field in the form.

With each iteration of field its meant to input into the fields_invoices table a invoice_id, field_id, entered_value

Here is my form

<?php echo $this->Form->create('FieldsInvoice'); ?>
    <?php foreach ($fields as $field): ?>
    <?php echo $this->Form->hidden('FieldsInvoice.id'); ?>
    <?php echo $this->Form->hidden('FieldsInvoice.field_id', array('default' =>$field['Field']['id'])); ?>
    <?php echo $this->Form->input('FieldsInvoice.invoice_id', array('default' =>$invoice_id, 'type'=>'hidden')); ?>
    <?php echo $this->Form->Input('FieldsInvoice.entered_value',  array('label'=>$field['Field']['name'], 'default' =>$field['Field']['default_value'])); ?>
    <?php endforeach ;?>
    <?php echo $this->Form->End('Submit');?>

Here is the related controller

    public function create($id)
    {   
    $this->set('title_for_layout', 'Create Invoice');
    $this->set('stylesheet_used', 'homestyle');
    $this->set('image_used', 'eBOXLogoHome.png');   
    $this->layout='home_layout';


     if (!is_numeric($id)) throw new BadMethodCallException('I need an ID');
     $this->Invoice->id = $id;
     if (!$this->Invoice->exists()) throw new NotFoundException('Invalid ID');

    $this->set('invoice_id',$id);

    $names = $this->Invoice->find('list',array(
    'fields'=>array('template_id'),
    'conditions'=>array('id'=>$id)));

    $fields = $this->Field->find('all', array(
      'conditions'=>array(
     'template_id'=>$names)));

    $this->set(compact('fields'));
    $this->set(compact('invoice_id'));

    $this->set('name',$names);
    $this->Invoice->create();
    if(empty($this->data)){
        $this->data= $this->Field->read($id);
    } 
    else{
        if($this->request->is('post'))
        {
            //die(debug($this->data));

            $this->Invoice->create();
            if($this->FieldsInvoice->save($this->request->data, array('deep'=>true)));
            {
            $this->Session->setFlash('The field has been updated');
            $this->redirect(array('controller'=>'invoices', 'action'=>'index'));

            }
            //else{
            $this->Session->setFlash('Could not be saved');
            //}
        }
    }
}









\app\Controller\InvoicesController.php (line 134)
array(
    'FieldsInvoice' => array(
        (int) 0 => array(
            'id' => '',
            'field_id' => '0',
            'invoice_id' => '97',
            'entered_value' => '1'
        ),
        (int) 1 => array(
            'id' => '',
            'field_id' => '99',
            'invoice_id' => '97',
            'entered_value' => '2'
        ),
        (int) 2 => array(
            'id' => '',
            'field_id' => '999',
            'invoice_id' => '97',
            'entered_value' => '3'
        ),
        (int) 3 => array(
            'id' => '',
            'field_id' => '9999',
            'invoice_id' => '97',
            'entered_value' => '4'
        )
    )
)

1 Answer 1

3

To post multiple fields, use:

foreach ($fields as $idx => $field):
    echo $this->Form->hidden('FieldsInvoice.'.$idx.'.id');   
    echo $this->Form->hidden('FieldsInvoice.'.$idx.'.field_id', ...);
    ...

and then use $this->FieldsInvoice->saveMany() (instead of ->save()) to save multiple records.

Sign up to request clarification or add additional context in comments.

4 Comments

you forgot some dots. For instance, 3entered_value in your debug means that you wrote 'FieldsInvoice.'.$idx.'entered_value' instead of 'FieldsInvoice.'.$idx.'.entered_value'.
updated with new array printout, however its now not saving anything to my databse.
Read the manual! Use $this->FieldsInvoice->save($this->request->data['FieldsInvoice']
Right. It should have been saveMany() instead of save() in my last comment. saveAll also works but is deprecated I think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.