CakePHP 3 - 在多个关联表上保存相同的ID

时间:2017-02-09 01:04:25

标签: php cakephp

我有一个'添加'用户'用户'需要将数据插入四个不同表的控制器。表之间的关系设置如下: db_design

箭头表示属于'属于'关系。

如果你看不到图片,关系是:用户所属的Imobiliarias属于Proprietarios所属的PessoalDados拥有一个用户。有点像圆圈或循环。

我的代码目前是这样的:

if ($this->request->is('post')) {
    $user = $this->Users->patchEntity($user, $this->request->data, [
        'associated' => [
            'Imobiliarias',
            'Imobiliarias.Enderecos',
            'Imobiliarias.Proprietarios',
            'Imobiliarias.Proprietarios.PessoalDados'
        ]
    ]);

    if ($this->Users->save($user)) {
        $this->Flash->success(__('The user has been saved.'));
        return $this->redirect(['action' => 'index']);
    }
    $this->Flash->error(__('The user could not be saved. Please, try again.')); 
}

问题是:我似乎无法将“用户”与“用户”链接起来。桌子和'Proprietarios'表格在PessoalDados'中的同一行。我试过$this->request->data['pessoal_dado'] = $this->request->data['imobiliaria']['proprietario']['pessoal_dado'],但它在数据库表中插入了两行,这不是我想要的。我试图做的是:

if ($this->request->is('post')) {
    `$this->request->data['pessoal_dado'] = $this->request->data['imobiliaria']['proprietario']['pessoal_dado']`
    $user = $this->Users->patchEntity($user, $this->request->data, [
        'associated' => [
            'Imobiliarias',
            'Imobiliarias.Enderecos',
            'Imobiliarias.Proprietarios',
            'Imobiliarias.Proprietarios.PessoalDados',
            'PessoalDados'
        ]
    ]);

    if ($this->Users->save($user)) {
        $this->Flash->success(__('The user has been saved.'));
        return $this->redirect(['action' => 'index']);
    }
    $this->Flash->error(__('The user could not be saved. Please, try again.')); 
}

请问,如何链接“用户”和“用户”。桌子和'Proprietarios'表到了PessoalDados'正在保存的数据?

0 个答案:

没有答案