Return a query object for the given type.
public createQueryForType ( string $type ) : Neos\Flow\Persistence\QueryInterface | ||
$type | string | |
return | Neos\Flow\Persistence\QueryInterface |
/** * Finds an object from the repository by searching for its identity properties. * * @param array $identityProperties Property names and values to search for * @param string $type The object type to look for * @return object Either the object matching the identity or NULL if no object was found * @throws DuplicateObjectException if more than one object was found */ protected function findObjectByIdentityProperties(array $identityProperties, $type) { $query = $this->persistenceManager->createQueryForType($type); $classSchema = $this->reflectionService->getClassSchema($type); $equals = []; foreach ($classSchema->getIdentityProperties() as $propertyName => $propertyType) { if (isset($identityProperties[$propertyName])) { if ($propertyType === 'string') { $equals[] = $query->equals($propertyName, $identityProperties[$propertyName], false); } else { $equals[] = $query->equals($propertyName, $identityProperties[$propertyName]); } } } if (count($equals) === 1) { $constraint = current($equals); } else { $constraint = $query->logicalAnd(current($equals), next($equals)); while (($equal = next($equals)) !== false) { $constraint = $query->logicalAnd($constraint, $equal); } } $objects = $query->matching($constraint)->execute(); $numberOfResults = $objects->count(); if ($numberOfResults === 1) { return $objects->getFirst(); } elseif ($numberOfResults === 0) { return null; } else { throw new DuplicateObjectException('More than one object was returned for the given identity, this is a constraint violation.', 1259612399); } }
/** * Returns a query for objects of this repository * * @return QueryInterface * @api */ public function createQuery() { $query = $this->persistenceManager->createQueryForType($this->entityClassName); if ($this->defaultOrderings !== []) { $query->setOrderings($this->defaultOrderings); } return $query; }
/** * Checks if the given value is a unique entity depending on it's identity properties or * custom configured identity properties. * * @param mixed $value The value that should be validated * @return void * @throws InvalidValidationOptionsException * @api */ protected function isValid($value) { if (!is_object($value)) { throw new InvalidValidationOptionsException('The value supplied for the UniqueEntityValidator must be an object.', 1358454270); } $classSchema = $this->reflectionService->getClassSchema(TypeHandling::getTypeForValue($value)); if ($classSchema === null || $classSchema->getModelType() !== ClassSchema::MODELTYPE_ENTITY) { throw new InvalidValidationOptionsException('The object supplied for the UniqueEntityValidator must be an entity.', 1358454284); } if ($this->options['identityProperties'] !== null) { $identityProperties = $this->options['identityProperties']; foreach ($identityProperties as $propertyName) { if ($classSchema->hasProperty($propertyName) === false) { throw new InvalidValidationOptionsException(sprintf('The custom identity property name "%s" supplied for the UniqueEntityValidator does not exists in "%s".', $propertyName, $classSchema->getClassName()), 1358960500); } } } else { $identityProperties = array_keys($classSchema->getIdentityProperties()); } if (count($identityProperties) === 0) { throw new InvalidValidationOptionsException('The object supplied for the UniqueEntityValidator must have at least one identity property.', 1358459831); } $identifierProperties = $this->reflectionService->getPropertyNamesByAnnotation($classSchema->getClassName(), 'Doctrine\\ORM\\Mapping\\Id'); if (count($identifierProperties) > 1) { throw new InvalidValidationOptionsException('The object supplied for the UniqueEntityValidator must only have one identifier property @ORM\\Id.', 1358501745); } $identifierPropertyName = count($identifierProperties) > 0 ? array_shift($identifierProperties) : 'Persistence_Object_Identifier'; $query = $this->persistenceManager->createQueryForType($classSchema->getClassName()); $constraints = [$query->logicalNot($query->equals($identifierPropertyName, $this->persistenceManager->getIdentifierByObject($value)))]; foreach ($identityProperties as $propertyName) { $constraints[] = $query->equals($propertyName, ObjectAccess::getProperty($value, $propertyName)); } if ($query->matching($query->logicalAnd($constraints))->count() > 0) { $this->addError('Another entity with the same unique identifiers already exists', 1355785874); } }