【问题标题】:polymorphic relations setup?多态关系设置?
【发布时间】:2016-08-20 02:18:54
【问题描述】:

我喜欢创建多态关系,但我不确定我的做法是否正确?

我有description_groups 表,它属于许多descriptions

关于laravel的多态关系,customersorders等表可以有很多descriptions

这是我想出的数据库架构:

description_groups表:

+----+----------------+
| id | name           |
+----+----------------+
|  1 | Stock Movement |
+----+----------------+

descriptions表:

description_groups 属于下面列出的许多descriptions

+----+----------------------+--------+
| id | description_group_id | name   |
+----+----------------------+--------+
|  1 |                    1 | Name 1 |
|  2 |                    1 | Name 2 |
|  3 |                    1 | Name 3 |
|  4 |                    1 | Name 4 |
+----+----------------------+--------+

使用polymorphic_table 表我可以定义哪个表和条目可以有描述。表名应该是什么?例如:

+----+----------------+------------+----------+
| id | description_id | table_name | table_id |
+----+----------------+------------+----------+
|  1 |              4 | customers  |        2 |
|  2 |              2 | orders     |       10 |
+----+----------------+------------+----------+

customers table:

+----+-----------------+
| id | name            |
+----+-----------------+
|  1 | Customer Name 1 |
|  2 | Customer Name 2 |
|  3 | Customer Name 3 |
+----+-----------------+

所以这意味着Customer Name 2 具有属于Stock Movement 条目的Name 4 条目描述。

【问题讨论】:

  • 表名不应该是一个表,它应该是代表该表的模型的路径。 App\CustomerApp\Order 很好。此外,不应将其称为 table_name,而应将 entity_nametable_id 称为 entity_id。更简洁,更直接,遵循 Laravel 的设计编译指示。
  • @Ohgodwhy 谢谢,请随时发布您的答案。 descriptionsdescription_groups 你觉得不错?

标签: laravel laravel-5.2


【解决方案1】:

Laravel 已经内置了对多态关系的支持,你可以在here找到更多。

我真的不明白您为什么要以这种方式设置架构,但是我会这样做,以便客户和订单可以有描述。

descriptions ( <id>, name, content, describable_type, describable_id )
customers (<id>, name)
orders (<id>, items)

注意descriable_type 是一个字符串,descriable_id 是一个无符号整数。

接下来,您必须按照文档中的说明设置关系(注意 cmets 告诉您它们属于哪个模型文件):

// In App\Description
public function describable() 
{
   return $this->morphTo();     
}

// In App\Customer
public function descriptions()
{
   return $this->morphMany('App\Description', 'describable');
}

// In App\Orders
public function descriptions()
{
   return $this->morphMany('App\Description', 'describable');
}

现在,这是 Laravel 文档没有提到的一件事;一对一多态关系的创建方式与一对一普通关系的创建方式相同,而一对多多态关系的创建方式与一对多普通关系的创建方式相同......(只需将morphTo 视为多态belongsTo)

所以要使用这个:

// be sure to set the correct $guarded access before using create()
$description = Description::create(['name' => 'Name', 'content' =>'Lorem Ispum";
$customer = Customer::create(['name' => 'Customer 1']);
$customer->describable()->associate($description);
$customer->save();

【讨论】:

  • 非常感谢@Extrakun。这很有帮助。我不明白为什么我们有descriable_id 时需要description.iddescriptions 表就像与description_groups 表链接的预定义列表。所以基本上,如果用户从组中选择Stock Movement,然后显示描述列表。用户从描述列表中选择任何一个,然后保存到数据透视表中。有意义吗?
  • “可描述”不是指描述,而是指描述所描述的内容。因此,在您的情况下,它指的是客户或订单。 describable_id 是它所属的客户或订单的id,describable_type 存储它是客户还是订单。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-20
  • 2013-05-13
  • 2018-04-25
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多