首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CakePHP hasOne relationship多重储蓄

CakePHP hasOne relationship多重储蓄
EN

Stack Overflow用户
提问于 2014-03-17 20:44:04
回答 1查看 103关注 0票数 0

我需要一些帮助的hasOne关系,是保存多个记录使用相同的外键。这种关系是:

Personal belongsTo UserAccount

UserAccount hasOne Personal

在名为personals的表中,我将外键放入user_accounts,称为user_account_id,只有在手动选择此外键作为唯一的数据库时,限制才有效。

我认为CakePHP可以处理这种情况。还是我漏掉了什么?

谢谢你的帮忙!

编辑

在我的add表单中,我有行echo $this->Form->input('UserAccount.id', array('type' => 'hidden', 'value' => $useraccount_id));,其中$useraccount_id是从我的UserAccountsController中提取的

代码语言:javascript
复制
 public function new_personal() {

    $current_user = $this->Auth->user('id');
    $current_account = $this->UserAccount->find('first', array(
        'conditions' => array('UserAccount.user_id' => $current_user)));
    $this->set('useraccount_id', $current_account['UserAccount']['id']);

    if ($this->request->is('post')) {
        if (!empty($this->request->data)) {
            unset($this->UserAccount->Personal->validate['user_account_id']);
            $this->UserAccount->saveAssociated($this->request->data);
            $this->Session->setFlash(__('Ok'));
            return $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Error'));
    }
}

我还尝试将unique => true属性添加到hasOne和belongsTo关系中(在我的模型上),但是它没有工作。我找到的唯一方法是直接在我的数据库中设置唯一的键。

EN

回答 1

Stack Overflow用户

发布于 2014-03-18 08:03:26

必须设置个人行持续编辑的ID。附加设置表中的唯一键d字段user_account_id

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22464963

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档