我开始为两种语言(de,en)开发一些内容管理系统,这些系统开始变得更大。
在这种情况下,我有帖子和页面(有点像WordPress或实际上像WordPress)。但是我还计划添加更多内容类型,例如评论,课程,教程(食谱),甚至可以购买的电子书。
所有这些对象通常都将它们可符合人< /strong>,因此它们会以其专用URL的方式显示在前端,例如 /posts /{slug},用于帖子, /{slug}, /{slug},for pages, /评论/{slug}以进行评论等。
在后端,这意味着为这些内容类型提供自动保存和修订系统。
因此,这将为我们提供以下选项:
- 单个表继承(我们需要以许多无效的值生活) - 不受Laravel正式支持,但有一个软件包。
- 多表继承(Laravel中也不支持)
- 多态性,该多态性受支持
- CMS解决方案(如Craft CMS),基本上会在元素,条目,字段中分解逻辑依此类推,替代方法将是Drupal的节点方法 - 我相信(我没有6-9个月的时间撰写从头表中的CMS)
- 具有每个型号的cms),并尝试使用尽可能多的逻辑通过使用特质(当前状态,我不太喜欢它...)。
经过一番谷歌搜索,在stackoverflow上搜索并查看其他项目后,我正在考虑以下结构:
内容表:
id
site_id
title
... (some more columns that are shared among all models)
contentable_type
contentable_id
posts
id
id
home
课程
id
name
featured
difficulty
free
因此,这些表将通过属于属于的关系链接到内容表,内容模型将定义可变形的关系。
class Content extends Eloquent {
public function contentable(){
return $this->morphTo();
}
}
class Post extends Eloquent (or Content) {
public function content(){
return $this->morphOne('Content', 'contentable');
}
}
使用模型将意味着您总是必须加载内容关系。
分类&amp;订购必须由JOINS执行。
当然,当创建创建时,我们必须首先创建内容类型模型,然后将其保存并归因于内容模型。
我以前从未实现过具有(sub)逻辑的系统,对我来说,只有一个ID的邮票表对我来说有点奇怪(对于其他内容类型,例如“ abots”,如果它们不don'是正确的t有额外的专栏),但我认为这将是解决此问题的“ Laravel”,对吗?
我相信STI对这种情况不起作用,这也与Laravel的雄辩模式有点相反。
有人已经经历过这种方法了吗?我在这里正确的轨道吗?
注意:我受到了这里的讨论的启发:
I started to develop a little Content Management System for two languages (de, en) that starts to grow bigger.
In that context, I have Posts and Pages (a bit like WordPress or actually just like WordPress). But I am also planning to add further content types like Reviews, Courses, Tutorials (Recipes) and maybe even E-Books that are purchasable.
All these objects would have in common that they are contentable, so they will be shown on the front end with their dedicated urls, like /posts/{slug} for posts, /{slug} for pages, /reviews/{slug} for reviews and so on.
On the backend, this means an auto save and revision system is offered for these content types.
So, this would leave us will the following options:
- Single Table Inheritance (we would need to live with many null values) - not supported officially by Laravel, but there is a package.
- Multi Table Inheritance (which is not supported in Laravel either)
- Polymorphism which is supported
- CMS solution (like craft CMS), which basically breaks up the logic in elements, entries, fields and so on, alternative approach would be drupal's node approach - out of scope I believe (I dont have 6-9 months time to write a CMS from the scratch)
- Have on Table per Model and try to use as much logic as possible between the models by using Traits (current status, I don't like it that much...).
After some googling, searching here on stackoverflow and looking at other projects, I am thinking of the following structure:
contents table:
id
site_id
title
... (some more columns that are shared among all models)
contentable_type
contentable_id
posts
id
pages
id
home
courses
id
name
featured
difficulty
free
So, these tables would be linked to the contents table through a belongsTo relationship, and the content model would define the morphable relationship.
class Content extends Eloquent {
public function contentable(){
return $this->morphTo();
}
}
class Post extends Eloquent (or Content) {
public function content(){
return $this->morphOne('Content', 'contentable');
}
}
Working with models would mean you would always have to load the content relationships.
Sorting & Ordering must be performed by joins.
And when creating, of course, we have to first create the content type model and then save and attribute it to a content model.
I never implemented a system with that kind of (sub) logic before, and it feels a bit odd to me to have a posts table with just an id (same would be true for other content types e.g. "abouts" in case they don't have extra columns), but I think it would be the "Laravel way" to solve this issue, right?
I believe STI wouldn't work for this case, and it is also a bit against Laravel's Eloquent pattern.
Has somebody already experiences with this approach? Am I on the right track here?
Note: I got inspired by the discussion here: How can I implement single table inheritance using Laravel's Eloquent?
发布评论
评论(1)
如果有人找到这个问题。我终于决定反对这种方法,基本上是因为我认为这是不值得的(而且大多数包装都无法使用)。
使用特质和cetera来重复使用尽可能多的逻辑并遵循雄辩的ORM方法,这是一种更好的方法。
In case anyone finds this question. I finally decided against this approach, basically because I believe it is not worth the efforts (and also most of the packages won’t work out of the box).
It is a much better approach to use Traits et cetera to reuse as much logic as possible and follow the Eloquent ORM approach.