【发布时间】:2016-01-14 11:27:00
【问题描述】:
我有一个包含多对多类别和标签的内容模型。
类别和标签存储在同一个表中,带有布尔 tag 标志:
category_id | name | tag
1 | Products | 0
2 | bestsellers | 1
我的内容模型具有如此定义的条件关系:
public function categories() {
return $this->belongsToMany('Foothing\Content\Z\Entities\Category\Eloquent\Category', 'content_content_categories', 'cid', 'tid')->where('tag', false);
}
public function tags() {
return $this->belongsToMany('Foothing\Content\Z\Entities\Category\Eloquent\Category', 'content_content_categories', 'cid', 'tid')->where('tag', true);
}
虽然tag 标志在读取操作上正常工作,即
$categories = Content::find(1)->categories;
$tags= Content::find(1)->tags;
它在sync 操作中没有按预期工作,实际上是以下代码
$content->categories()->sync(1, 2, 3);
将同步整个表格,无论tag 标志如何:标签将被销毁,我的内容将仅与类别 1、2、3 相关。
这种方法有什么不好的地方吗?
【问题讨论】:
标签: php laravel laravel-5 eloquent