There is no way to make the in condition for a query into a PostgreSQL table with a service (enum) field. Values indicate existing, this is no problem. I tried to do implode, but queryBuilder escapes the entire string, I tried to pass an array, but I get an array to string conversion and an Array parameter instead of 'ACTIVE', 'ANOTHER'. How correctly in queryBuilder to make a condition in for enum?
public function query() { return $this->db()->createQueryBuilder(); } public function getActiveSubscriberServices($msisdn) { $result = $this->query() ->select('id') ->from('subscriber.service') ->where('state in (?)') ->setParameter(0, [ 'ACTIVE', 'ANOTHER', ]) ->execute(); return $result->fetchAll(); } I received a FROM subscriber.service WHERE (state in (?)) 'With params [[\ "ACTIVE \", \ "ANOTHER \"]]: \ n \ nSQLSTATE [22P02]: Invalid text representation : 7 ERROR: invalid input value for enum subscriber.service_state: \ "Array \"
I try to do it like this
->where($this->query()->expr()->in('state', [ 'ACTIVE', 'ANOTHER', ])) And I get this in: IN (ACTIVE, ANOTHER) , which does not work, because the values inside in must be escaped.
Now I use the code below, but this is not flexible and a bit of a crutch.
$services = [Service::STATE_ACTIVE, Service::STATE_ACTIVATING]; $result = $this->query() ->select('*') ->from('subscriber.service') ->where($this->query()->expr()->in('state', array_map(function($item) { return '?'; }, array_keys($services)))) ->setParameters($services) ->andWhere('msisdn = ?') ->setParameter(count($services), $msisdn) ->execute(); return $result->fetchAll();