将数据保存到2个具有多个关系的表中时,cakephp 3.x数据不会保存

时间:2016-01-07 15:44:39

标签: cakephp-3.0 has-many cakephp-model

我有两个表客户端和分支机构在它们之间有字段client_id关系我们客户端hasMany分支和分支属于用户,我在我的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.'));
            }
        }
?>

但是保存在客户端表中而不是分支表中的数据请告诉我id应该做什么或者我的错误是什么 抱歉我的英文不好

1 个答案:

答案 0 :(得分:1)

如果客户hasHany分支,则应该

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

阅读关于它的manual