" IN"具有条件过滤的谓词不起作用 [英] "IN" predicate with Criteria filtering isn't working
问题描述
我正试图用标准,以避免在仅搜索某些元素时收集所有的加载。
public function bar()
{
$ entity = ...; //对于这个例子没有用
$ property = ...; //对于这个例子没有用,但是一个集合的getter是
$ ids = [22,13]; //只是为了这个例子
$ 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(我将只显示最后一个和相关的部分)
AND te.id =?'with params [22,13]:
所以,这个错误
注意:数组到字符串转换
我想在这里生成错误的DQL,但我不知道为什么。
任何人都有线索?
这是我试图在
$ b $上匹配的集合属性b / **
* @ 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;
编辑
如果我尝试转储 $条件
对象,我获得这个
标准{#10958▼
-expression:比较{#10956▼
-field:id
-op:IN
-value:Value {#10948▼
-value:数组:2 [▼
0 => 22
1 => 13
]
}
}
-orderings:[]
-firstResult:null
-maxResults:null
}
所以这似乎是正确的。我也知道我可以使用或表达式,但我宁愿明白为什么会出现这个问题。
Edit2
我已更改 $条件
如下
$ criteria = Criteria :: create()
- > where(Criteria :: expr() - > eq('id',22))
- > orWhere(Criteria :: expr() - > eq('id',13));
现在,查询是正确的,但收集不是:它是空的,但不应该如果我从mysql cli手动运行该查询,我获得了我的期望)。
所以...
奖金问题
只有在集合尚未加载的情况下,才可以在实体捕获者或存储库内使用 Criteria
/ p>
Edit3 - 奖金问题
似乎如果您没有加载集合,匹配条件很漂亮无用。事实上,如果我循环到集合元素并调用一些getter,然后我使用标准,结果集合是我正在搜索(但是,当然,所有集合元素现在加载)
我发现这个
https://github.com/doctrine/doctrine2/issues/4910
似乎是在ManyToMany收集情况中的知名问题
I'm trying to filter a doctrine collection directly with Criteria in order to avoid all collection-loading when I'm searching only for certain elements.
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);
}
Above code will produce this DQL (I'll show only last and relevant part)
AND te.id = ?' with params [22,13]:
And so, this error
Notice: Array to string conversion
I suppose that wrong DQL is generated here, but I don't know why.
Does anyone got a clue?
This is the collection property I'm trying to match on
/**
* @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;
Edit
If I try to dump $criteria
object, I obtain this
Criteria {#10958 ▼
-expression: Comparison {#10956 ▼
-field: "id"
-op: "IN"
-value: Value {#10948 ▼
-value: array:2 [▼
0 => 22
1 => 13
]
}
}
-orderings: []
-firstResult: null
-maxResults: null
}
So this seems to be correct. I'm also aware I could use "or" expressions, but I would prefer to understand why this issue is taking place.
Edit2
I've changed $criteria
as follows
$criteria = Criteria::create()
->where(Criteria::expr()->eq('id', 22))
->orWhere(Criteria::expr()->eq('id', 13));
As now, query is correct but collection isn't: it is empty but shouldn't be (if I run manually that query from mysql cli I obtain what I expect).
So ...
BONUS QUESTION
Can I use Criteria
only inside entities getters or repository if I collection isn't already loaded?
Edit3 - Bonus question
It seems that if you don't have collection already loaded, matching criteria is pretty useless. Infact if I loop onto collection elements and call some getters and, then, I use criteria, resulting collection is what I was searching for (but, of course, ALL collection elements are now loaded)
I've found this
https://github.com/doctrine/doctrine2/issues/4910
It seems that is a "well-known" issue in ManyToMany collection situations
这篇关于" IN"具有条件过滤的谓词不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!