views:

403

answers:

1

Hi GUys,

Im trying to save a record to a table aswell as an accociated record. The main table is called quotes and it has a hasmany link to the table quote_items. quote_items belongsto quote

When i try and save it saves the record in quote but does not save the record in quote_items .

Below is my quote add function

    function add() {
    if (!empty($this->data)) {
        $this->Quote->create();
        if ($this->Quote->saveAll($this->data)) {
            $this->Session->setFlash(__('The Quote has been saved', true));
            //$this->redirect(array('action'=>'index'));
        } else {
            $this->Session->setFlash(__('The Quote could not be saved. Please, try again.', true));
        }
    }
    $this->Quote->recursive = 2;
    $statuses = $this->Quote->Status->find('list');
    $contacts = $this->Quote->Contact->find('list');
    $this->set(compact('statuses', 'contacts'));
}

Quote view / form setup

    <?php echo $form->create('Quote', array('action' => 'add'));?>
    <fieldset>
        <legend><?php __('Add Quote');?></legend>
    <?php
        echo $form->input('Quote.name');
        echo $form->input('Quote.revision');
        echo $form->input('Quote.status_id');
        echo $form->input('Quote.contact_id');
        echo $form->input('quote_item.product_id');
        echo $form->input('quote_item.name');
        echo $form->input('quote_item.price');
        echo $form->input('quote_item.description');
        echo $form->input('Quote.totalcost');
    ?>
    </fieldset>
<?php echo $form->end('Submit');?>

Array that is returned when the form is submitted

    Array ( 
    [quote] => Array ( 
                [name] => Test 
                [revision] => 1 
                [status_id] => 1 
                [contact_id] => 1 
                [totalcost] => 123 
              ) 

    [quote_item] => Array ( 
                [product_id] => 1
                [name] => test 
                [price] => 123 
                [description] => tes 1234 
              ) 
) 

This seems to follow exactly what is listed in the cakephp documentation so i cant work out why its not working - http://book.cakephp.org/view/84/Saving-Related-Model-Data-hasOne-hasMany-belongsTo

Thanks in advance

+2  A: 

The correct way to build the form would be:

echo $form->input('Quote.name');
...
echo $form->input('QuoteItem.0.product_id');
echo $form->input('QuoteItem.0.name');
...
echo $form->input('QuoteItem.1.product_id');
echo $form->input('QuoteItem.1.name');

The resulting array should look like this:

array(
    'Quote' => array(
        'name' => 'Test'
        ....
    ),
    'QuoteItem' => array(
        0 => array(
            'product_id' => 1
            'name' => 'test'
            ...
        )
        1 => array(
            'product_id' => 2
            'name' => 'test'
            ...
        )
    )
)

According to naming conventions, model names are camelized (ModelName, not model_name). Also, since Quote hasMany QuoteItems, the QuoteItem array needs to consist of many QuoteItem arrays. Hope that makes sense. :)

deceze
Makes Perfect sense, I came so close when i was tyring to fix it but i didnt pickup on needing to get rid of the underscores. Thankyou so much :D
kwhohasamullet