多态关系设置?(polymorphic relations setup?)

编程入门 行业动态 更新时间:2024-10-28 21:29:39
多态关系设置?(polymorphic relations setup?)

我喜欢创建多态关系,我不确定我的情况是否正确?

我有description_groups表,它属于许多descriptions

关于laravel多态关系, customers和orders等表可以有很多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具有Name 4条目描述,其属于Stock Movement条目。

I like to create polymorphic relations and I am not sure if I am doing the right way in my case?

I have description_groups table and it is belong to many descriptions

In regarding to laravel polymorphic relation, table such as customers and orders can have many descriptions

Here is database schema I have came up with:

description_groups table:

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

descriptions table:

description_groups is belong to many descriptions listed below

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

Using polymorphic_table table I can define which table and entry can have description. what the table name should be? For example:

+----+----------------+------------+----------+ | 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 | +----+-----------------+

So this mean Customer Name 2 have Name 4 entry description which is belong to Stock Movement entry.

最满意答案

Laravel已经建立了对多态关系的支持,你可以在这里找到更多信息 。

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

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

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

接下来,您将必须设置关系,如文档中所述(请注意告诉您属于哪个模型文件的注释):

// 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();

Laravel has built in support for polymorphic relationship, which you can find out more here.

I do not really understand why you set up your schema the way you did, however this is how I would do it such that customers and orders can have descriptions.

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

Note that descriable_type is a string, and descriable_id is an unsigned integer.

Next you will have to set up the relationship, as described in the docs (note the comments which tell you which model file they belongs to):

// 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'); }

Now, here's one thing that the Laravel docs don't mention; a one-to-one polymorphic relationship is created the same way as a one-to-one normal relationship, while a one-to-many polymorphic relationship is created the same way as a one-to-many normal relationship...(just think of morphTo as a polymorphic belongsTo)

So to use this:

// 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();

更多推荐

本文发布于:2023-08-03 14:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1391225.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:关系   多态   polymorphic   relations   setup

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!