/**
  * 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 mixed Either the object matching the identity or FALSE if no object was found
  * @throws \RuntimeException if more than one object was found
  * @author Robert Lemke <*****@*****.**>
  * @author Karsten Dambekalns <*****@*****.**>
  */
 protected function findObjectByIdentityProperties(array $identityProperties, $type)
 {
     $query = $this->queryFactory->create($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();
     if (count($objects) === 1) {
         return current($objects);
     } elseif (count($objects) === 0) {
         return FALSE;
     } else {
         throw new \RuntimeException('More than one object was returned for the given identity, this is a constraint violation.', 1259612399);
     }
 }
Пример #2
0
 /**
  * Returns a query for objects of this repository
  *
  * @return \F3\FLOW3\Persistence\QueryInterface
  * @author Robert Lemke <*****@*****.**>
  * @author Karsten Dambekalns <*****@*****.**>
  * @api
  */
 public function createQuery()
 {
     return $this->queryFactory->create($this->objectType);
 }