cakephp-3.0has-manycakephp-model

cakephp 3.x data not saving when Saving data into 2 tables with has many relation


I have two tables clients and branches branches has field client_id relation between them us client hasMany Branches and branch belongs to user, i have following code in my clients/add.ctp view file

<?php 
    echo  $this->Form->create($client); 
    echo $this->Form->input('name');
    echo $this->Form->input('branch.0.branch_name');
    echo $this->Form->input('branch.0.email');
    echo $this->Form->input('profile_link');

?>

and my controller code isas follow
<?php
    public function add() {

        $client = $this->Clients->newEntity();
        if ($this->request->is('post')) {

            $client = $this->Clients->patchEntity($client, $this->request->data, [
                'associated' => ['Branches']
            ]);
            if($this->Clients->save($client)) {
                $this->Flash->success(__('data has been saved.'));
            } else {
                $this->Flash->error(__('The data could not be saved. Please, try again.'));
            }
        }
?>

but data saved in the client table but not in branch table please tell me what should id do or whats my error is sorry for my bad english


Solution

  • if client hasHany branches you should do

    echo $this->Form->input('branches.0.branch_name');
    echo $this->Form->input('branches.0.email');
    

    read the manual about it