Php 具有多态关系的Laravel
whereHas方法似乎不太管用Php 具有多态关系的Laravel,php,mysql,laravel,laravel-4,polymorphic-associations,Php,Mysql,Laravel,Laravel 4,Polymorphic Associations,whereHas方法似乎不太管用 $res = Entreprise::whereHas('labels',function ($q) { $q->where('hidden','!=',1); }) ->whereHas('labels',function ($q) { $q->whereHidden(1); }) ->get(); dd(count($res)); //shows in
$res = Entreprise::whereHas('labels',function ($q)
{
$q->where('hidden','!=',1);
})
->whereHas('labels',function ($q)
{
$q->whereHidden(1);
})
->get();
dd(count($res)); //shows int 2
以下是标签关系:
public function labels()
{
return $this->morphToMany('Label', 'labelable');
}
这是数据库:
id | nom | deleted_at | created_at | updated_at | junior_id | label_type_id | abbreviation | id_siaje | hidden
6 | Environnord | 0000-00-00 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | 1 | 4 | EnvNord | 0 | 1
7 | Salon créer | 0000-00-00 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | 1 | 4 | Créer | 0 | 1
8 | Salon WebAnalytics | 0000-00-00 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | 1 | 4 | Web | 0 | 0
当我这样做时:
$res = Entreprise::whereHas('labels',function ($q)
{
$q->where('hidden','!=',1);
$q->whereHidden(1);
})->get()
dd(count($res)); //int 0
我得到了期望值
在我的数据库中,Enterprise对象没有超过1个标签,因此标签要么隐藏要么不隐藏,因此其中一个条件应该为false
编辑
这是可贴标签的桌子
+----+----------+--------------+----------------+-----------+
| id | label_id | labelable_id | labelable_type | junior_id |
+----+----------+--------------+----------------+-----------+
| 1 | 1 | 925 | Etude | 1 |
| 2 | 2 | 926 | Etude | 1 |
| 3 | 3 | 927 | Etude | 1 |
| 4 | 2 | 927 | Etude | 1 |
| 5 | 1 | 928 | Etude | 1 |
| 6 | 2 | 928 | Etude | 1 |
| 7 | 3 | 929 | Etude | 1 |
| 8 | 2 | 931 | Etude | 1 |
| 9 | 1 | 933 | Etude | 1 |
| 10 | 2 | 934 | Etude | 1 |
| 11 | 4 | 1 | User | 1 |
| 12 | 5 | 2 | User | 1 |
| 13 | 7 | 1 | Entreprise | 1 |
| 14 | 6 | 2 | Entreprise | 1 |
| 15 | 7 | 3 | Entreprise | 1 |
| 16 | 8 | 4 | Entreprise | 1 |
| 17 | 6 | 5 | Entreprise | 1 |
| 18 | 7 | 6 | Entreprise | 1 |
| 19 | 6 | 7 | Entreprise | 1 |
+----+----------+--------------+----------------+-----------+
正如您所看到的,问题可能是它们是两个labelable_id为1的实体,以及两个labelable_id为2的实体。但这是一个变体,所以雄辩的用户应该知道,标签上的用户不应该被考虑在内
当我查看生成的SQL时:
select * from `entreprises`
where `entreprises`.`deleted_at` is null
and `entreprises`.`junior_id` = ?
and (select count(*) from `labels`
inner join `labelables` on `labels`.`id` = `labelables`.`label_id`
where `labels`.`deleted_at` is null
and `labels`.`junior_id` = ?
and `labelables`.`labelable_id` = `entreprises`.`id`
and `hidden` != ?
and `hidden` = ?
and `labels`.`deleted_at` is null
and `labels`.`junior_id` = ?) >= ?
这看起来像是
标签。没有考虑标签类型,因此这可能是问题的根源。虽然我不知道问题是什么,但我打赌你确实得到了正确的答案,你的期望是不正确的
第二个示例使用了一个,其中的显然不返回任何行,因为该条件永远不可满足
然而,首先,我想你的理解是有缺陷的。生成的查询反映了您使用两个whereHas
子句指定的内容。它将查找所有至少有一个隐藏标签和至少有一个未隐藏标签的企业。因为这是一个多人的关系,这实际上是令人满意的
请注意,这与第二个示例不同,在第二个示例中,您搜索至少有一个隐藏和未隐藏标签的所有企业
我不知道enterprises表,也不知道many-many-join表,但我想这两个结果实际上满足了我上面提到的条件。您的数据库引擎可能没有说谎。如果您不这么认为,并且感觉生成的查询实际上是错误的,请告诉我们您认为错误的地方。您的问题是什么?是“return$this->morphToMany('Label','labelable')/*->select('问题中有一个输入错误,或者这是真正的代码?嘿,我的问题是,鉴于企业在我的数据库中只有一个标签,我不明白为什么第一个语法显示结果,而第二个语法不显示结果。@marcanuy:这只是一个输入错误,我已经编辑了代码。嘿,我理解为什么这两段代码在通用da中的行为不一样ta(例如一个企业可以有一个隐藏的标签和一个可见的标签)。但在我的例子中(参见标签的数据库转储),一个企业一次只有一个标签,所以我不明白为什么这不起作用。这似乎是因为一个企业和一个用户有相同的可标签id,但它仍然很奇怪,因为它是多态关系。我明白了。那么这确实是Laravel的多态关系的haswere
中的一个bug。你可以在JU上提交一个bugt为通过搜索到达此处的任何其他人添加对bug报告的引用,