31

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?

diegowc
  • 455
  • 2
  • 14
Preciel
  • 2,666
  • 3
  • 20
  • 45

1 Answers1

66

You can do

$em->getRepository(Entity::class)->findBy(array('id' => $idList));
Noémi Salaün
  • 4,866
  • 2
  • 33
  • 37
  • Oh... was as simple as this... I simply assumed that it wasn't possible that way... Thanks – Preciel Sep 20 '18 at 15:48
  • 4
    It wasn't until pretty recently. – mblaettermann Sep 21 '18 at 02:26
  • 2
    @Preciel the documentation sucks, however this is at least vaguely documented here: https://www.doctrine-project.org/projects/doctrine-orm/en/2.14/reference/working-with-objects.html#by-simple-conditions `If you pass an array of values Doctrine will convert the query into a WHERE field IN (..) query automatically:` – jave.web Mar 22 '23 at 11:30