The predicate "IN" with filtering criteria does not work

I am trying to filter a collection of doctrines directly using Criteria to avoid all loading the collection when I search only for specific items.

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); } 

The above code will create this DQL (I will show only the last and relevant part)

And te.id =? 'with parameters [22,13]:

So this error

Note. Convert array to string

I assume that the wrong DQL is being created here, but I do not know why.

Anyone got it?




This is a collection property that I am trying to map to

 /** * @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 the $criteria object, I get 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 that seems right. I also know that I can use the expressions "or", but I would prefer to understand why this question is relevant.

Edit2

I changed $criteria as follows

 $criteria = Criteria::create() ->where(Criteria::expr()->eq('id', 22)) ->orWhere(Criteria::expr()->eq('id', 13)); 

As now, the request is correct, but the collection is not: it is empty, but it should not be (if I manually start this request from mysql cli, I get what I expect).

So...

QUESTION OF BONUS

Can I use Criteria only inside getters or repository objects if the collection is not already loaded?

Edit3 - Bonus Question

It seems that if you don't already have a collection already loaded, suitable criteria are pretty useless. Infact, if I loop around the elements of the collection and call some getters and then use the criteria, the resulting collection is what I was looking for (but, of course, now all the elements of the collection are loading)

+3
doctrine doctrine2
Feb 12 '16 at 9:07
source share
1 answer

I found this

https://github.com/doctrine/doctrine2/issues/4910

This seems to be a “well-known” issue in ManyToMany collection situations.

+1
Feb 12 '16 at 15:14
source share
— -



All Articles