【发布时间】:2015-03-03 14:16:19
【问题描述】:
我正在使用 Laravel 构建一个 PHP 应用程序,其中我有一个 Comment 模型和许多其他可以附加 Comment 的模型。
例如,Profile 可以有多个Comment,但Post 可以有多个Comment。
我应该将Comment 分成两个模型(ProfileComment 和PostComment)还是应该像这样将它们放在一起?
class Comment {
public function user()
{
return $this->hasOne('User');
}
public function profile()
{
return $this->belongsTo('Profile');
}
public function post()
{
return $this->belongsTo('Post');
}
}
如果我将它们放在一起,那么在数据库级别我应该如何管理comments 表?
我正在考虑拥有以下列:
integer: id - auto-incrementing id
integer: user_id - the user id
integer: foreign_id - profile/post id
varchar: type - which model? profile or post?
varchar: content - the actual comment
这是错误的方法吗?
【问题讨论】:
标签: php database-design model-view-controller laravel-4 orm