/** * Check for uniqueness by a set of criteria. * * @param EntityInterface $entity * @param array $criteria Keys are fields to check, values are strings to * check against. An entity may be passed as a value. * @return bool */ public function isUnique(EntityInterface $entity, array $criteria) { $qb = $this->getEntityManager()->createQueryBuilder(); $qb->select('e.id')->from($this->getEntityClass(), 'e'); // Exclude the passed entity from the query if it has an persistent // indentifier. if ($entity->getId()) { $qb->andWhere($qb->expr()->neq('e.id', $this->createNamedParameter($qb, $entity->getId()))); } foreach ($criteria as $field => $value) { $qb->andWhere($qb->expr()->eq("e.{$field}", $this->createNamedParameter($qb, $value))); } return null === $qb->getQuery()->getOneOrNullResult(); }