Thursday, September 26, 2013

Re: Cakephp not able to save associated model

Hi Gaurav Kumar

In your add method in the controller:
1. remove the fields option for the retrieval of policies. CakePHP automatically takes id and name when using find(list).

In your add view, the form input for policies will assume that policies belongs in CloudApp.
1. Change to ... input('Policy.id', array('options' => $policies, 'multiple' => true)) ...
The above will tell CakePHP that the values in $policies is to be used for populating the dropdown list for policies.
See also the CakePHP documentation at:
http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-select-checkbox-and-radio-inputs

Kindly tell us of the result you get.
Enjoy, John

On Thursday, 26 September 2013 04:31:20 UTC+3, Gaurav Kumar wrote:
(X-posting from SO)

I've a model `CloudApp` which has HABTM relationship with `Policy` model e.g:

    class CloudApp extends AppModel {
        public $displayField = 'name';        
        public $hasAndBelongsToMany = array(
            'Policy' => array(
                'className' => 'Policy',
                'joinTable' => 'cloudapp_policies',
                'foreignKey' => 'cloud_app_id',
                'associationForeignKey' => 'policy_id',
                'unique' => 'keepExisting',
            )
        );
    
    }

`Policy` model looks like this- 

    class Policy extends AppModel {
            public $belongsTo = 'CloudApp'; 
    }




When I use scaffolding, everything works fine- I am able to multi-select policies for CloudApp that I am saving. 


But when I try to manually save data, associated data in Policy model is not getting saved.


Here is my `add` controller- 

public function add() {
    $this->loadModel('Policy');
    $this->set('policies', $this->Policy->find('list', array(
                'fields' => array('Policy.name')
    )));

    if ($this->request->is('post')) {
        pr($this->request->data);
        $this->CloudApp->saveAll($this->request->data);        }
}

add.ctp looks like this-


    echo $this->Form->create('CloudApp');
    ?>
    <fieldset>
        <legend><?php echo __('Add Application'); ?></legend>
        <?php    
        echo $this->Form->input('name');
        echo $this->Form->input('policies', array(
            'multiple' => true
        ));    
        
        ?>
    </fieldset>
    <?php echo $this->Form->end(__('Submit')); ?>

Output of  `pr($this->request->data);` is- 

    Array
    (
        [CloudApp] => Array
            (
                [name] => 123
                [policies] => Array
                    (
                        [0] => 18
                        [1] => 19
                        [2] => 20
                    )
    
            )
    
    )


What am I doing wrong?

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: