代码之家  ›  专栏  ›  技术社区  ›  Deepak Goyal

cakephp 3.x数据在将数据保存到2个表时不保存

  •  0
  • Deepak Goyal  · 技术社区  · 9 年前

    我有两个表clients和branchs,它们之间有字段client_id关系,我们clienthasMany branches和branch属于用户,我的clients/add中有以下代码。ctp视图文件

    <?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.'));
                }
            }
    ?>
    

    但是数据保存在客户机表中而不是分支表中,请告诉我我应该做什么或者我的错误是什么 对不起我英语不好

    1 回复  |  直到 9 年前
        1
  •  1
  •   arilia    9 年前

    如果客户端 hasHany 你应该做的分支

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

    阅读 manual 关于它