【问题标题】:Phalcon: Storing related records (Implicit Transactions) issuePhalcon:存储相关记录(隐式事务)问题
【发布时间】:2014-07-13 08:07:29
【问题描述】:

我有 3 个表/模型。他们有1-1的关系。每个用户只有一个个人资料和一个联系地址。

当访问者在我的网站上注册一个新帐户时,我将在每个用户、个人资料、地址表中创建新记录。为了确保数据完整性,我想我必须在这里使用事务。所以我在 Phalcon 文档中使用了“存储相关记录(隐式事务)”来执行此操作,但遇到了一个问题。

一个新记录插入地址,但配置文件中的地址 fk 始终为空?一切正常,但aid fk 始终为空。

User:
-uid
-email
-password
-(other records)

Profile:
-uid
-aid (address id)
-name
-age
-phone
-(other records)

Address:
-aid
-province
-district
-(other records)

class User extends Model {
    public function initialize() {
        $this->hasOne('uid', 'Profile', 'uid');
    }
}

class Profile extends Model {
    public function initialize() {
        $this->hasOne('uid', 'User', 'uid');
        $this->hasOne('aid', 'Address', 'aid');
    }
}

class Address extends Model {
    public function initialize() {
        $this->hasOne('aid', 'Profile', 'aid');
    }
}

如何解决这个问题?或者我必须手动使用独立事务? 谢谢你的帮助! :-)

【问题讨论】:

    标签: phalcon


    【解决方案1】:

    您不能在创建用户之后但在创建配置文件之前添加地址吗?然后 Profile 表应该可以访问 uid 和aid。

    在我看来,它只是按照它们创建的顺序工作,然后不会返回进行更新。或者,您也可以在地址表中添加一个 uid 列,这样您就可以通过它进行映射。如果您在某些时候只需要用户和地址而不是个人资料,这也可能很方便。

    【讨论】:

      猜你喜欢
      • 2023-01-24
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-24
      • 2020-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多