【发布时间】:2017-11-20 18:53:08
【问题描述】:
有时我们使用 MySql Views 来组织相关的表,以便于搜索和排序。例如,如果您有带有状态和来源的帖子。
Post
subject
body
source_id
status_id
Status
id
label
other_field
Source
id
label
other_field
View
create view read_only_posts as
SELECT statuses.label as status, sources.label as source, posts.*
from posts
left join statuses on statuses.id = posts.status_id
left join sources on sources.id = posts.source_id
然后我们有 Post 模型和一个额外的模型:
// Post.php
class Post extends Model
{
//
}
// ReadOnlyPost.php
class ReadOnlyPost extends Post
{
protected $table = 'read_only_posts';
}
这很好,因为现在您可以直接将状态或源作为字符串而不是 id 进行排序或过滤。您还可以包含“other_field”。
但我们有一个问题需要帮助。如果您在 Posts 上有一个多态的多对多关系,我无法让它在只读版本上工作。例如,如果您有多态标签:
// Post.php Model
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}
问题是当你过滤带有特定标签的帖子(使用只读模型)时,你会得到这样的 sql:
从存在的
read_only_posts中选择计数(*)作为聚合(从tags内部连接taggables中选择*tags.id=taggables.taggable_id其中read_only_posts.@ 987654333@ =taggables.taggable_type和taggables.taggable_type= 'read_only_posts' 和label= '测试')
如您所见,问题在于 taggables.taggable_type = 'read_only_posts'。
我找不到覆盖模型的变形类型的方法。 (我在 laravel 5.4 上,MorphClass 不再存在了)。变形图是一个关联数组,所以你不能这样做:
// AppServiceProvider
public function boot()
{
Relation::morphMap([
'posts' => Post::class,
'posts' => ReadOnlyPost::class, <--- Can't do this
我的愚蠢解决方法是,当我将标签附加到帖子时,我还将它附加到 ready_only_posts,这有点混乱。
还有其他人将视图用于只读模型吗?任何人都有更好的方法来覆盖特定模型的多对多多态类型?
【问题讨论】:
标签: mysql laravel laravel-5 polymorphism