Mysql Laravel多态关系问题

Mysql Laravel多态关系问题,mysql,laravel,laravel-4,Mysql,Laravel,Laravel 4,在此建议不要使用多态关系 例如,对于此Sql命令,我们必须不使用外键 CREATE TABLE Comments ( comment_id SERIAL PRIMARY KEY, comment TEXT NOT NULL, issue_type VARCHAR(15) NOT NULL CHECK (issue_type IN (`Bugs`, `Features`)), issue_id INT NOT NULL, FOREIGN KEY i

在此建议不要使用多态关系

例如,对于此Sql命令,我们必须不使用
外键

CREATE TABLE Comments (

    comment_id SERIAL PRIMARY KEY,

    comment TEXT NOT NULL,

    issue_type VARCHAR(15) NOT NULL CHECK (issue_type IN (`Bugs`, `Features`)),

    issue_id INT NOT NULL,

    FOREIGN KEY issue_id REFERENCES ???

);
那么我们必须:

CREATE TABLE Comments (

    comment_id SERIAL PRIMARY KEY,

    comment TEXT NOT NULL,

    issue_type VARCHAR(15) NOT NULL CHECK (issue_type IN (`Bugs`, `Features`)),

    issue_id INT NOT NULL,

);
此命令在使用
JOIN
或同时使用
JOIN
时出现问题,例如:

SELECT * FROM Comments c

LEFT JOIN Bugs b ON (c.issue_type = 'Bugs' AND c.issue_id = b.issue_id)

LEFT JOIN Features f ON (c.issue_type = 'Features' AND c.issue_id = f.issue_id);
这些问题仅适用于
SELECT
其他问题有:
UPDATE
DELETE

解决这个问题的方法是什么

更新:
现在如何找到帖子所有者?

Laravel具有多态关系,请参见此处:

您的数据库表的设置是相同的(除了需要使用
commentable_id
commetable_type
来处理以下代码示例),并且在您的模型中执行以下操作:

class Comment extends Eloquent {

    public function commentable()
    {
        return $this->morphTo();
    }

}

class Bug extends Eloquent {

    public function comments()
    {
        return $this->morphMany('Comment', 'commentable');
    }

}

class Feature extends Eloquent {

    public function comments()
    {
        return $this->morphMany('Comments', 'commentable');
    }

}
然后,您可以像这样使用:

$bug = Bug::find(1)->comments();
你也可以选择另一种方式,因此,如果你只是抓取一个评论列表,你就可以在不知道它是什么的情况下找到该评论的所有者:

$comment = Comment::find(1);

$commentable = $comment->commentable; 
// this will load the bug or feature that the comment belongs to

现在如何找到帖子的所有者?更新了答案,在底部添加了一个部分来概括这一点