【发布时间】: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