When you’re utilizing Doctrine to search a database for a value not present in a given set, it might be tempting to use some sloppy code like the following:
$excuse = ExcuseTable::getInstance() ->createQuery('e') ->where('e.id NOT IN ('.implode(',', $arrayOfAlreadyUsedIds).')') ->fetchOne();
While code like this can work, it’s difficult to read and error prone. Just think if the given array has data that needs to be escaped; You’ll have to tack on a call to array map in there too, making your code all the more messy. There’s just no excuse for that kind of thing.
Instead, write cleaner code and let Doctrine do all the hard work for you: (Notice the change on line 3.)
$excuse = ExcuseTable::getInstance() ->createQuery('e') ->whereNotIn('e.id', $arrayOfAlreadyUsedIds)) ->fetchOne();
Note: the syntax for ->whereIn works exactly the same way.