当一个关系"可能"如何在Laravel中创建一个多态关系?不存在?

时间:2014-04-21 01:23:25

标签: php laravel-4 relationship eloquent polymorphic-associations

我有一个帖子表,一个帖子可能有一个媒体。它可以是视频或画廊。这很容易,我所做的是建立了一个多态关系并且它起作用了。

+----------------------------------------------------------+
| posts                                                    |
+------+-----------------+---------+----------+------------+
| id   | title           | content | media_id | media_type |
+------+-----------------+---------+----------+------------+
| 1    | Some Awesome... | Rea...  | 10       | Video      |
+------+-----------------+---------+----------+------------+
| 2    | Cool Photos...  | See...  | 12       | Gallery    |
+------+-----------------+---------+----------+------------+
| 3    | Watch The...    | See...  | 22       | Video      |
+------+-----------------+---------+----------+------------+

问题是某些帖子没有媒体和多态关系将记录连接到其类型字段中指定的模型。

+----------------------------------------------------------+
| posts                                                    |
+------+-----------------+---------+----------+------------+
| id   | title           | content | media_id | media_type |
+------+-----------------+---------+----------+------------+
| 1    | Some Awesome... | Rea...  | 10       | Video      |
+------+-----------------+---------+----------+------------+
| 2    | Cool Photos...  | See...  | 12       | Gallery    |
+------+-----------------+---------+----------+------------+
| 3    | Watch The...    | See...  | 22       | Video      |
+------+-----------------+---------+----------+------------+
| 4    | Top 10 Games... | Play... | 0        | ?          |
+------+-----------------+---------+----------+------------+
| 5    | Do You Want...  | Buil... | 0        | ?          |
+------+-----------------+---------+----------+------------+

将media_type设置为空会导致错误,因为它会搜索空字符串类。建立这种关系的最佳方式是什么?我应该加入一些假桌子(请不要)?或者有没有正确的方法来解决这个问题?

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为您的问题的最佳解决方案是分隔帖子表和多态关系表。例如,您的帖子表仅包含帖子信息,关系表包含post_id和关系信息,因此您无需担心媒体是否存在。像这样,

+----------------------------------|
| posts                            |
+------+-----------------+---------|
| id   | title           | content |
+------+-----------------+---------|
| 1    | Some Awesome... | Rea...  |
+------+-----------------+---------|
| 2    | Cool Photos...  | See...  |
+------+-----------------+---------|
| 3    | Watch The...    | See...  |
+------+-----------------+---------|

+----------------------------------------+
| media                                  |
+------+---------+----------+------------|
| id   | post_id | media_id | media_type |
+------+---------+----------+------------|
| 1    | 1       | 10       | Video      |
+------+---------+----------+------------|
| 2    | 2       | 12       | Gallery    |
+------+---------+----------+------------|
| 3    | 3       | 22       | Video      |
+------+---------+----------+------------|

在媒体模型中

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

在Post模型中,它与Media模型是一对一或一对多的关系(无论你喜欢什么)。

public function media()
{
    return $this->hasMany('Media');
}

OR

public function media()
{
   return $this->hasOne('Media');
}

在视频,图库模型中,它们与媒体模型具有多态关系。

public function media()
{
return $this->morphMany('Media', 'medias');
}