【问题标题】:I am confused with Sails.js waterline one-to-one association logic我对 Sails.js 水线一对一关联逻辑感到困惑
【发布时间】:2015-02-28 21:42:01
【问题描述】:

所以我很困惑,因为我是一名 PHP 开发人员,并且经常使用 Laravel 和 FuelPHP

我真正不明白的是它本身的关联。

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

用户有一个个人资料

个人资料属于用户

我习惯了以下构建(Laravel 风格)

用户表

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

Users_profile 表

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"
    }

  }
};

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

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 不起作用(注意是用于收藏)

那么,谁能给我一个关于 Laravelis 风格的逻辑示例?

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

【问题讨论】:

    标签: node.js orm associations sails.js waterline


    【解决方案1】:

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

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

    无论如何,如果您确实需要它,您可以使用.afterCreate lifecycle callback 来确保双向链接,例如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 以在创建配置文件时更新受影响的用户。

    【讨论】:

    • 不知道这个问题。感谢您的提醒和详细的回答
    • 也没有意识到这是一个问题。您会认为这值得在文档中提及...
    • 现在有一个专门的entire page in the docs
    猜你喜欢
    • 1970-01-01
    • 2014-03-24
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多