public function findOneBy($key, $value) { $instances = $this->findBy($key, $value); if (count($instances) > 1) { throw new \Exception('Expected only one result, got ' . count($instances)); } return isset($instances[0]) ? $instances[0] : null; }