我很困惑与Sails.js水线一到一关联逻辑 [英] I am confused with Sails.js waterline one-to-one association logic

查看:613
本文介绍了我很困惑与Sails.js水线一到一关联逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么,为什么IM困惑,因为我是一个PHP开发人员和使用Laravel和FuelPHP很多的原因。

So the reason why im confused, because I am a PHP developer and used Laravel and FuelPHP alot

我不真正了解是联想它的自我。

What i dont really understand is the association it self.

我的意思,我想创造一个基本的hasOne /逻辑的belongsTo,具有以下

What i mean, i wanted to create a basic hasOne / BelongsTo logic, with the following

用户有一个配置文件

档案属于一个用户

我用下面的建立(Laravel风格)

I am used to the following build up (Laravel style)

用户表

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  

然后我定义的模型这样

Then i just defined models this way

用户模型

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');
    }
}

和它只是工作,因为返回$这个 - > hasOne(个人资料); 会自动检查为 USER_ID

And it just works, because the return $this->hasOne('profile'); will auto check for the user_id

尝试过同样的Sails.js(在帆路)

Tried the same in Sails.js (in the sails way)

用户模型

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

  }
};

和从文档阅读现在我已经更新我的表这样

And reading from the documentation now i have to update my table this way

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的,我真的不知道为什么。

So i need to store 2 more id's again, and i do not really get why.

比我更深入地阅读试图用通过没有工作(注意,是集合)

Than i read further tried to use via did not work (noted that is for collections)

所以,任何人都可以给我一个逻辑的例子为Laravelis风格?

So, anybody could give me a logic example for a Laravelis style?

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

Foud nothing about this in the docs (a more easier way), because in my opinion if the user will have more relations, this will cause and ID hell (just my aopinion)

推荐答案

这是一个已知问题的帆不完全支持一个​​to-one关联;你必须设置你希望能够从填充任何一侧的外键。也就是说,如果你想拥有用户#1连接到简介#1和能够做 User.find(1).populate('轮廓'),你会设置简介 用户#1,但这并不自动意味着做 Profile.find(1).populate(用户)将工作。这是相对于多到多的在帆,其中添加一侧上的链路是足够的关系。这是因为的对许多的关系用一个连接表,而一对一的关系没有。

It is a known issue that Sails doesn't fully support one-to-one associations; you have to set the foreign key on whichever side you want to be able to populate from. That is, if you want to have User #1 linked to Profile #1 and be able to do User.find(1).populate('profile'), you would set the profile attribute of User #1, but that doesn't automatically mean that doing Profile.find(1).populate('user') will work. This is as opposed to many-to-many relationships in Sails, where adding the link on one side is sufficient. That's because to-many relationships use a join table, whereas to-one relationships do not.

这并没有在帆一直是一个优先级的理由是,一对一的关系,通常不是非常有用。除非你有一个真正令人信服的理由不这样做,你最好只是合并这两个模型为一体。

The reason this hasn't been a priority in Sails is that one-to-one relationships are usually not really useful. Unless you have a really compelling reason for not doing so, you're better off just merging the two models into one.

在任何情况下,如果这是你真正需要的东西,你可以使用<$c$c>.afterCreate生命周期回调以确保双向链接,例如 user.js的

In any case, if it's something you really need, you can use the .afterCreate lifecycle callback to ensure a bi-directional link, for example in 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 来处理时,更新受影响的用户配置文件已创建。

You could add a similar .afterCreate() to Profile.js to handle updating the affected user when a profile was created.

这篇关于我很困惑与Sails.js水线一到一关联逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆