The proper way to build conditionnal SQL is to use the query builder

```php
public function listOrders(array $baseIds, $offsetStart = 0, $perPage = 10, $sort = null)
{
    $qb = $em->createQueryBuilder();

    $qb
       ->select('o'))
       ->from('Entities\OrderElement', 'e')
       ->from('Entities\Order', 'o')
       ->where(($qb->expr()->eq('e.order', 'o'));

    if (!empty($baseIds)) {
        $qb->andWhere($qb->expr()->in('e.base_id', $baseIds))
    }

    $qb->groupBy('o.id');

    if ($sort === 'user') {
        $qb->orderBy('o.user_id', 'ASC');
    } else if ($sort === 'usage') {
        $qb->orderBy('o.usage', 'ASC');
    } else {
        $qb->orderBy('o.created_on', 'ASC');
    }

    $qb
        ->setFirstResult((int) $offsetStart)
        ->setMaxResults(max(10, (int) $perPage));

    return $qb->getQuery()->getResult();
}
```