PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, February 21, 2022

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

 February 21, 2022     cakephp-3.0, cakephp-model, has-many     No comments   

Issue

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



Answered By - arilia
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing