Doctrine orm &引用;在;带条件过滤的谓词不';行不通
我正试图直接使用筛选原则集合,以避免在仅搜索某些元素时加载所有集合Doctrine orm &引用;在;带条件过滤的谓词不';行不通,doctrine-orm,doctrine,Doctrine Orm,Doctrine,我正试图直接使用筛选原则集合,以避免在仅搜索某些元素时加载所有集合 public function bar() { $entity = ...; // not useful for the example $property = ...; // not useful for the example but is a getter for a collection $ids = [22, 13]; // just for the sake of this example
public function bar()
{
$entity = ...; // not useful for the example
$property = ...; // not useful for the example but is a getter for a collection
$ids = [22, 13]; // just for the sake of this example
$criteria = Criteria::create()->where(Criteria::expr()->in("id", $ids));
return $this->foo($entity, $property, $criteria);
}
public function foo($entity, $property, Criteria $criteria)
{
return $this->propertyAccessor->getValue($entity, $property)->matching($criteria);
}
上面的代码将生成这个DQL(我将只显示最后一个相关部分)
和te.id=?'以及参数[22,13]:
所以,这个错误
注意:数组到字符串的转换
我想这里生成了错误的DQL,但我不知道为什么
有人有线索吗
这是我试图匹配的集合属性
/**
* @ORM\ManyToMany(targetEntity="Vendor\Bundle\Entity\Foo")
* @ORM\JoinTable(name="foo_bar",
* joinColumns={@ORM\JoinColumn(name="foo_bar_id", referencedColumnName="id", onDelete="cascade")},
* inverseJoinColumns={@ORM\JoinColumn(name="foo_id", referencedColumnName="id")}
* )
*/
protected $foo;
编辑
如果我尝试转储$criteria
对象,我将获得此
Criteria {#10958 ▼
-expression: Comparison {#10956 ▼
-field: "id"
-op: "IN"
-value: Value {#10948 ▼
-value: array:2 [▼
0 => 22
1 => 13
]
}
}
-orderings: []
-firstResult: null
-maxResults: null
}
所以这似乎是正确的。我也知道我可以使用“或”表达,但我更愿意理解为什么会发生这个问题
编辑2
我将$criteria
更改如下
$criteria = Criteria::create()
->where(Criteria::expr()->eq('id', 22))
->orWhere(Criteria::expr()->eq('id', 13));
现在,查询是正确的,但集合不是:它是空的,但不应该是空的(如果我从mysql cli手动运行该查询,我将获得我所期望的结果)
所以
奖金问题
如果我的集合尚未加载,我是否可以仅在实体getter或存储库中使用条件
Edit3-奖金问题
看起来,若您还并没有加载集合,那个么匹配条件是非常无用的。事实上,如果我循环到集合元素并调用一些getter,然后,我使用条件,得到的集合就是我要搜索的(当然,所有集合元素现在都已加载)我发现了这一点
在许多收集情况下,这似乎是一个“众所周知”的问题