Symfony Doctrine’s version of MySQL’s “IN” — as in “values contained in”

MySQL’s “IN” query filter is very useful. Basically, if you have a list of record ID numbers, you can query them all neatly like so:

$query="SELECT field_name,field_name FROM table_name WHERE id IN (3,7,11,13,17)"

The containing comma-delimited list is a simple and neat query that can be assembled in your code quickly.

Duplicating MySQL’s “IN( )” in Doctrine is just as easy, you just have to give the “findBy” method an array based on the pattern:

array(field(s)=>array(id_val,id_val,id_val…) )

Here’s a basic example to help illustrate:

$resultsArr= $em->getRepository('repositoryName')->findBy(array('id' => array(1, 2, 3)));

I thought it was too easy, thanks to Stack Overflow for showing me the way!

About Author:

Senior Cloud Software Engineer and 25+ years experienced video production, video editing and 3D animation services for a variety of global clients including local video production here in Jacksonville, Florida.

Leave a Comment

Your email address will not be published. Required fields are marked *