I am confused with one-on-one communication logic with Sails.js

So, the reason why I'm confused is because I'm a PHP developer and used Laravel and FuelPHP alot

What I really don't understand is his association.

What I mean, I wanted to create the hasOne / BelongsTo core logic with the following

User has one profile.

User owned profile

I'm used to the next creation (Laravel style)

User 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  

Then I just defined the models this way

User model

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

Profile model

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

    protected $primaryKey = 'user_id';

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

And it just works because it return $this->hasOne('profile');will automatically checkuser_id

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

User model

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

  },
};

Profile model

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 now from the documentation I have to update the 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

So, I need to save 2 more identifiers again, and I really don't understand why.

, via, (, )

, - ?

( ), , , , ( )

+4
1

, Sails ; , . , User # 1, Profile # 1 User.find(1).populate('profile'), Profile User # 1, , Profile.find(1).populate('user') . " " "", . , to-many , "-" .

, Sails, , "--" . , , .

, , .afterCreate , , , 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 ID
      return Profile.update({id: values.profile}, {user: values.id}).exec(cb);
    }

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

.afterCreate() Profile.js .

+7

Source: https://habr.com/ru/post/1569713/


All Articles