Redbeanphp一对一关系

Redbeanphp一对一关系,php,orm,one-to-one,relation,redbean,Php,Orm,One To One,Relation,Redbean,在我的项目中,我的数据库中有两个实体,即用户和帐户。一个用户有一个帐户。用户表有帐户id字段作为引用帐户实体的外键。 我正在为用户自动生成一个新帐户。一切都按照我的预期创建。但是我如何才能引用一个特定用户的帐户呢。这是我的注册脚本 public function register ($data = null) { if ($data != null) { try { $user = R::dispense('user');

在我的项目中,我的数据库中有两个实体,即用户和帐户。一个用户有一个帐户。用户表有帐户id字段作为引用帐户实体的外键。 我正在为用户自动生成一个新帐户。一切都按照我的预期创建。但是我如何才能引用一个特定用户的帐户呢。这是我的注册脚本

public function register ($data = null)
{
    if ($data != null) {
        try {
            $user = R::dispense('user');
            $user->name = $data['name'];
            $user->lastname = $data['last-name'];
            $user->username = $data['username'];
            $user->password = $data['password'];
            $user->email = $data['email'];
            $user->city = $data['city'];
            $user->country = $data['country'];
            //create account for user
            $account = R::dispense('account');
            $account->account_no = $this->genAccountNumber();
            $account->money_sum = 0;
            $user->account = $account;
            $id = R::store($user);
            return $id;
        } catch (Exception $e) {
            echo $e->getMessage;
        }
    }
}

如果您能提供任何帮助,我们将不胜感激,只需将account_id字段作为外键添加到用户表中,而不是将account bean作为用户bean的外部引用

public function register ($data = null){
if ($data != null) {
    try {
        $user = R::dispense('user');
        $user->name = $data['name'];
        $user->lastname = $data['last-name'];
        $user->username = $data['username'];
        $user->password = $data['password'];
        $user->email = $data['email'];
        $user->city = $data['city'];
        $user->country = $data['country'];
        //create account for user
        $account = R::dispense('account');
        $account->account_no = $this->genAccountNumber();
        $account->money_sum = 0;
        $account_id = R::store($account);
        $user->account_id = $account_id;   // this is the line you have to change
        $id = R::store($user);
        return $id;
    } catch (Exception $e) {
        echo $e->getMessage;
    }
}}
当加载bean时,您可以直接从用户bean访问帐户实例 (如果存在一对一或一对多MAAP,则为约束)


你不需要改变任何事情-你的方式是正确的。线路

$user->account = $account;
是正确的方法-假设您在流体模式下运行,您的模式将自动更新,以通过这种方式为您创建外键关系

要访问该帐户,只需使用$user->account

echo $user->account;

将向您显示帐户bean的所有属性。

请参阅以获取参考。这是不正确的-进行您建议的更改不会为$user->account创建必要的外键关系。
echo $user->account;