/** * 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 \TYPO3\FLOW3\Property\Exception\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 = array(); 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 \TYPO3\FLOW3\Property\Exception\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 \TYPO3\FLOW3\Persistence\Doctrine\Query * @api */ public function createQuery() { $query = $this->persistenceManager->createQueryForType($this->entityClassName); if ($this->defaultOrderings !== array()) { $query->setOrderings($this->defaultOrderings); } return $query; }