1

hi all I'm trying to add information to the database using a hasMany with joins association

Invoice - id, sender_id, receiver_id, template_id
Field - id, name, description, default_value, template_id
fields_invoices - id, invoice_id, field_id, entered_value

here is the view

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

here is the 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');
            //}
        }
    }
}

here is whats printed when debug is used

\app\Controller\InvoicesController.php (line 134)
array(
    'FieldsInvoice' => array(
        (int) 87 => '',
        (int) 0 => '',
        'invoiceno' => 'test1',
        (int) 99 => '',
        'duedate' => 'test2',
        (int) 999 => '',
        'amount' => 'test3',
        (int) 9999 => '',
        'description' => 'test4'
    )
)

all the correct information is there but not submitting to the right place, the first int is the invoice_id, the other other int's are field_id and 'test1','test2','test3','test4' are the entered_value. somehow I need to code my view so that it saves an invoice_id,field_id,test1 in the same array, how can I acheive this?

1 Answer 1

1

Not sure why you are creating 2 hidden fields for each of the fields.

First read this: http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-related-model-data-hasone-hasmany-belongsto

echo $form->input('Account.0.name', array('label' => 'Account name'));
echo $form->input('Account.0.username');
echo $form->input('Account.0.email');

And then further reading: http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-hasmany-through-data

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

4 Comments

i dont want the user to touch the invoice_id or field_id
i also don't want the id to be apparent to the user
@user1393064 you do realize that they are still pretty much viewable by viewing source right?
it doesn't bother me at the moment, as long as it looks pretty. issue now is it only prints the last entered value

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.