我对 Sails.js waterline one-to-one 关联逻辑感到困惑

I am confused with Sails.js waterline one-to-one association logic

所以我之所以感到困惑,是因为我是一名 PHP 开发人员并且大量使用 Laravel 和 FuelPHP

我不太明白的是它自己的联想。

我的意思是,我想创建一个基本的 hasOne / BelongsTo 逻辑,具有以下内容

用户有一份个人资料

个人资料属于用户

我习惯了下面的搭建(Laravel风格)

用户table

id | username    | email     | password 
---------------------------------------
1  | My Username | My email  | 1234568

Users_profile table

user_id | first_name    | last_name      
----------------------------------------
1       | My First name | My Last name  

然后我就这样定义了模型

用户模型

class Users extends Eloquent 
{
    public function profile() 
    {
        return $this->hasOne('profile');
    }
}

个人资料模型

class Profile extends Eloquent 
{
    protected $tableName = 'users_profile';

    protected $primaryKey = 'user_id';

    public function user() 
    {
        return $this->belongsTo('User');
    }
}

它确实有效,因为 return $this->hasOne('profile'); 会自动检查 user_id

在 Sails.js 中尝试了相同的方法(在航行方式中)

用户模型

module.exports = {

  attributes: {

    username: {
        type: 'string',
        unique: true,
        required: true
    },

    email: {
        type: 'string',
        unique: true,
        email: true,
        required: true
    },

    password: {
        type: 'string',
        required: true
    },

    profile: {
      model: "profile",
    }

  },
};

个人资料模型

module.exports = {

    tableName: 'user_profile',

    autoPK: false,

    autoCreatedAt: false,

    autoUpdateddAt: false,

  attributes: {

    user_id: {
        type: 'integer',
        primaryKey: true
    },

    first_name: {
        type: 'string',

    },

    last_name: {
        type: 'string',

    },

    user: {
      model: "user"
    }

  }
};

现在阅读文档我必须以这种方式更新我的table

id | username    | email     | password | profile
-------------------------------------------------
1  | My Username | My email  | 1234568  | 1



user_id | first_name    | last_name    | user |
-----------------------------------------------
1       | My First name | My Last name |  1

所以我需要再存储 2 个 ID,我不太明白为什么。

比我进一步阅读尝试使用 via 没有用(注意这是针对 collections)

所以,有人可以给我一个 Laravelis 风格的逻辑示例吗?

在文档中对此一无所知(更简单的方法),因为在我看来,如果用户有更多的关系,这将导致 ID 地狱(只是我的意见)

Sails 不完全支持一对一关联是 known issue;您必须在您希望能够从中填充的任何一侧设置外键。也就是说,如果您想将 User #1 link 编辑为 Profile #1 并且能够执行 User.find(1).populate('profile'),您可以设置 profile 属性User #1,但这并不自动意味着 Profile.find(1).populate('user') 会起作用。这与 Sails 中的 多对多 关系相反,在 Sails 中,在一侧添加 link 就足够了。这是因为 对多 关系使用联接 table,而 对一 关系不使用。

这在 Sails 中不是优先考虑的原因是一对一关系通常不是很有用。除非你有一个 really compelling reason 不这样做,否则最好将两个模型合并为一个。

无论如何,如果这是你真正需要的东西,你可以使用.afterCreate lifecycle callback来确保双向link,例如User.js:

module.exports = {

  attributes: {...},

  afterCreate: function(values, cb) {

    // If a profile ID was specified, or a profile was created 
    // along with the user...
    if (values.profile) {
      // Update the profile in question with the user's ID
      return Profile.update({id: values.profile}, {user: values.id}).exec(cb);
    }

    // Otherwise just return
    return cb();
  }
};

您可以将类似的 .afterCreate() 添加到 Profile.js 以在创建配置文件时处理更新受影响的用户。