/** * @inheritDoc */ public function apply(FilterEvent $event, array $options) { $queryBuilder = $event->getQuery(); if (!$queryBuilder instanceof QueryBuilder) { throw new \Exception('Vardius\\Bundle\\ListBundle\\Filter\\Types\\EntityType supports only doctrine filters for now. To filter Propel or ElasticSearch Queries use callbacks or create your own FilterType classes'); } $value = $event->getValue(); if ($value instanceof ArrayCollection) { $value = $value->toArray(); } if ($value && !empty($value)) { $field = empty($options['field']) ? $event->getField() : $options['field']; $queryBuilder->{$options['joinType']}($event->getAlias() . '.' . $field, $field); if ($options['multiple']) { $value = is_array($value) ? $value : [$value]; $ids = []; foreach ($value as $entity) { $ids[] = $entity->getId(); } $value = $ids; $queryBuilder->andWhere($field . '.' . $options['property'] . ' IN(:vardius_entity_' . $event->getField() . ')'); } else { $queryBuilder->andWhere($field . '.' . $options['property'] . ' = :vardius_entity_' . $event->getField()); } $queryBuilder->setParameter('vardius_entity_' . $event->getField(), $value); } return $queryBuilder; }
/** * @inheritDoc */ public function apply(FilterEvent $event, array $options) { $queryBuilder = $event->getQuery(); if (!$queryBuilder instanceof QueryBuilder) { throw new \Exception('Vardius\\Bundle\\ListBundle\\Filter\\Types\\PropertyType supports only doctrine filters for now. To filter Propel or ElasticSearch Queries use callbacks or create your own FilterType classes'); } $value = $event->getValue(); if ($value) { $field = empty($options['field']) ? $event->getField() : $options['field']; $queryBuilder->andWhere($event->getAlias() . '.' . $field . ' = :vardius_property_' . $event->getField())->setParameter('vardius_property_' . $event->getField(), $value); } return $queryBuilder; }