Example #1
0
 /**
  */
 public function getOptions()
 {
     $classSchema = $this->reflectionService->getClassSchema($this->getRelationClass());
     if ($classSchema->getRepositoryClassName() !== NULL) {
         $repository = $this->objectManager->get($classSchema->getRepositoryClassName());
         $query = call_user_func(array($repository, $this->settings['QueryMethod']));
     } else {
         $query = $this->persistenceManager->createQueryForType($this->getRelationClass());
     }
     $options = $query->execute()->toArray();
     if ($this->settings['LabelPath'] !== NULL) {
         $options = array();
         foreach ($query->execute() as $option) {
             $identifier = $this->persistenceManager->getIdentifierByObject($option);
             $label = ObjectAccess::getPropertyPath($option, $this->settings['LabelPath']);
             $options[$identifier] = $label;
         }
     }
     if ($this->settings['EmptyOption'] !== NULL) {
         $newOptions = array('' => $this->settings['EmptyOption']);
         foreach ($options as $key => $value) {
             $newOptions[$key] = $value;
         }
         $options = $newOptions;
     }
     return $options;
 }
 /**
  * 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 = 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 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\Flow\Persistence\QueryInterface
  * @api
  */
 public function createQuery()
 {
     $query = $this->persistenceManager->createQueryForType($this->entityClassName);
     if ($this->defaultOrderings !== array()) {
         $query->setOrderings($this->defaultOrderings);
     }
     return $query;
 }
 /**
  * @param Client $client
  * @param string $className
  *
  * @return array
  */
 protected function getModificationsNeededStatesAndIdentifiers(Client $client, $className)
 {
     $query = $this->persistenceManager->createQueryForType($className);
     $states = array(ObjectIndexer::ACTION_TYPE_CREATE => array(), ObjectIndexer::ACTION_TYPE_UPDATE => array(), ObjectIndexer::ACTION_TYPE_DELETE => array());
     foreach ($query->execute() as $object) {
         $state = $this->objectIndexer->objectIndexActionRequired($object, $client);
         $states[$state][] = $this->persistenceManager->getIdentifierByObject($object);
     }
     return $states;
 }
 /**
  * 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 \TYPO3\Flow\Validation\Exception\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($value);
     if ($classSchema === NULL || $classSchema->getModelType() !== \TYPO3\Flow\Reflection\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 = array($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);
     }
 }
 /**
  * @return QueryInterface
  */
 protected function createQuery()
 {
     return $this->persistenceManager->createQueryForType($this->projectionClassName);
 }