createQuery('p') ->innerJoin('p.PlanRelations pr') ->addWhere('pr.plan_id = ?', $plan->getId()); if(is_null($level)) { $query->addWhere('pr.level_id = ?', $level->getId()); } return $query; } }