findBy with multiple IDs
Asked Answered
K

1

35

In my quest to edit data from the inverse side of a ManyToOne - OneToMany relation, and to avoid fetching the whole table's content, I want to fetch data from a list of IDs.

While this would work,

$data=array();
foreach($idList as $id) {
    array_push($data, $em->getRepository(Entity::class)->findBy(array('id', $id)));
}

It would do as many queries as there are IDs. Before making my own query in the repository, I would like to know if it's possible to use multiple IDs with findBy.

If it's possible, how do I do it?

Keyes answered 20/9, 2018 at 13:59 Comment(0)
K
69

You can do

$em->getRepository(Entity::class)->findBy(array('id' => $idList));
Konstanz answered 20/9, 2018 at 14:50 Comment(4)
Oh... was as simple as this... I simply assumed that it wasn't possible that way... ThanksKeyes
It wasn't until pretty recently.Crate
@Keyes the documentation sucks, however this is at least vaguely documented here: doctrine-project.org/projects/doctrine-orm/en/2.14/reference/… If you pass an array of values Doctrine will convert the query into a WHERE field IN (..) query automatically:Loveland
I receive the error Doctrine\ORM\Persisters\Entity\BasicEntityPersister::getSelectConditionStatementColumnSQL(): Argument #1 ($field) must be of type string, int given, called in /var/www/html/vendor/doctrine/orm/src/Persisters/Entity/BasicEntityPersister.php on line 165Lynseylynus

© 2022 - 2024 — McMap. All rights reserved.