用Laravel的双形态关系来命名约定.

问题描述 投票:0回答:1

当你在一个表(模型)上有两个形态关系时,Laravels中正确的命名规则是什么?

例子:

posts
    id - integer
    title - string
    body - text

videos
    id - integer
    title - string
    url - string

buyers
    id - integer
    name - string

sellers
    id - integer
    name - string

comments
    id - integer
    body - text
    commentable1_id - integer // link to video/posts
    commentable1_type - string // type link to video/posts
    commentable2_id - integer // link to buyers/sellers
    commentable2_type - string // type link to buyers/sellers

现在我使用 namme commentable1commentable2 但这不是一个很好的Laravel方式. 我怎样才能用正确的Laravel方式来做?

laravel polymorphic-associations
1个回答
0
投票

我想这更多的是个人喜好吧,如果我会做,我会这样做的

commentable1_id would be video_post_id
commentable1_type would be video_post_type
commentable2_id would be buyer_seller_id
commentable2_type would be buyer_seller_type

而这对另一个人来说可能是不同的,但对我来说,它是可以自我解释的,所以我会使用这个。考虑到你将保持你的主要模型名称不变,如果你将改变,它会相应地改变。

如果你想符合laravel标准。

比如你的视频(s)属于post(s), 那么列名就会简单的变成......。videoable_type和其他两列分别为 videoable_id, sellerable_idsellerable_type.

参考资料

© www.soinside.com 2019 - 2024. All rights reserved.