public function __construct(Context $context, IMapper $mapperOne, IMapper $mapperTwo, PropertyMetadata $metadata)
 {
     $this->context = $context;
     $this->mapperOne = $mapperOne;
     $this->mapperTwo = $mapperTwo;
     $this->metadata = $metadata;
     $parameters = $mapperOne->getManyHasManyParameters($mapperTwo);
     $this->joinTable = $parameters[0];
     if ($this->metadata->relationshipIsMain) {
         $this->targetRepository = $this->mapperTwo->getRepository();
         list($this->primaryKeyFrom, $this->primaryKeyTo) = $parameters[1];
     } else {
         $this->targetRepository = $this->mapperOne->getRepository();
         list($this->primaryKeyTo, $this->primaryKeyFrom) = $parameters[1];
     }
 }
Example #2
0
 private function parseCondition(array $levels, IMapper $mapper)
 {
     /** @var IDbStorageReflection $reflection */
     $reflection = $mapper->getStorageReflection();
     $expression = '';
     $column = array_pop($levels);
     $entityMD = $this->metadataStorage->get($mapper->getRepository()->getEntityClassNames()[0]);
     foreach ($levels as $level) {
         if (!$entityMD->hasProperty($level)) {
             throw new InvalidArgumentException("Undefined property {$entityMD->className}::\${$level}.");
         }
         $propertyMD = $entityMD->getProperty($level);
         if (!$propertyMD->relationshipRepository) {
             throw new InvalidArgumentException("Entity {$entityMD->className}::\${$level} does not contain a relationship.");
         }
         $targetMapper = $this->model->getRepository($propertyMD->relationshipRepository)->getMapper();
         $targetReflection = $targetMapper->getStorageReflection();
         if ($propertyMD->relationshipType === PropertyMetadata::RELATIONSHIP_ONE_HAS_MANY) {
             $table = $targetReflection->getStorageName();
             $joinColumn = $targetReflection->convertEntityToStorageKey($propertyMD->relationshipProperty);
             $expression .= ":{$table}({$joinColumn})";
         } elseif ($propertyMD->relationshipType === PropertyMetadata::RELATIONSHIP_MANY_HAS_MANY) {
             if ($propertyMD->relationshipIsMain) {
                 $expression .= ':' . $reflection->getManyHasManyStorageName($targetMapper);
                 $expression .= '.' . $reflection->getManyHasManyStoragePrimaryKeys($targetMapper)[1];
             } else {
                 $expression .= ':' . $targetReflection->getManyHasManyStorageName($mapper);
                 $expression .= '.' . $targetReflection->getManyHasManyStoragePrimaryKeys($mapper)[0];
             }
         } else {
             $expression .= '.' . $reflection->convertEntityToStorageKey($level);
         }
         $mapper = $targetMapper;
         $reflection = $targetReflection;
         $entityMD = $this->metadataStorage->get($mapper->getRepository()->getEntityClassNames()[0]);
     }
     // check if property exists
     $entityMD->getProperty($column);
     $column = $reflection->convertEntityToStorageKey($column);
     return "{$expression}.{$column}";
 }
Example #3
0
 public function __call($method, $args)
 {
     if (isset($this->proxyMethods[strtolower($method)])) {
         $result = call_user_func_array([$this->mapper, $method], $args);
         if (!($result instanceof ICollection || $result instanceof IEntity || $result === null)) {
             $result = $this->mapper->toCollection($result);
         }
         return $result;
     } else {
         return parent::__call($method, $args);
     }
 }
Example #4
0
 public function __call($method, $args)
 {
     if (isset($this->proxyMethods[strtolower($method)])) {
         if (substr($method, 0, 5) === 'getBy' || substr($method, 0, 6) === 'findBy') {
             return call_user_func_array([$this->findAll(), $method], $args);
         }
         $result = call_user_func_array([$this->mapper, $method], $args);
         if (!($result instanceof ICollection || $result instanceof IEntity)) {
             $result = $this->mapper->toCollection($result);
         }
         return $result;
     } else {
         return parent::__call($method, $args);
     }
 }
 /**
  * @param  string $condition
  * @param  string $direction
  * @return Closure
  */
 public function createSorter(array $conditions)
 {
     $columns = [];
     foreach ($conditions as $pair) {
         list($column, , $sourceEntity) = ConditionParserHelper::parseCondition($pair[0]);
         $sourceEntityMeta = $this->metadataStorage->get($sourceEntity ?: $this->mapper->getRepository()->getEntityClassNames()[0]);
         $columns[] = [$column, $pair[1], $sourceEntityMeta];
     }
     $getter = function ($element, $chain, EntityMetadata $sourceEntityMeta) use(&$getter) {
         $column = array_shift($chain);
         $propertyMeta = $sourceEntityMeta->getProperty($column);
         // check if property exists
         $value = $element->hasValue($column) ? $element->getValue($column) : NULL;
         if ($value instanceof IRelationshipCollection) {
             throw new InvalidStateException('You can not sort by hasMany relationship.');
         }
         if (!$chain) {
             return $value;
         } else {
             $targetEntityMeta = $this->metadataStorage->get($propertyMeta->relationship->entity);
             return $getter($value, $chain, $targetEntityMeta);
         }
     };
     return function ($a, $b) use($getter, $columns) {
         foreach ($columns as $pair) {
             $_a = $this->simplifyValue($getter($a, $pair[0], $pair[2]));
             $_b = $this->simplifyValue($getter($b, $pair[0], $pair[2]));
             $direction = $pair[1] === ICollection::ASC ? 1 : -1;
             if (is_int($_a) || is_float($_a)) {
                 if ($_a < $_b) {
                     return $direction * -1;
                 } elseif ($_a > $_b) {
                     return $direction;
                 }
             } else {
                 $res = strcmp((string) $_a, (string) $_b);
                 if ($res < 0) {
                     return $direction * -1;
                 } elseif ($res > 0) {
                     return $direction;
                 }
             }
         }
         return 0;
     };
 }
 private function fetchCounts(SqlBuilder $builder, array $values)
 {
     $targetStoragePrimaryKey = $this->targetMapper->getStorageReflection()->getStoragePrimaryKey()[0];
     $builder = clone $builder;
     $table = $builder->getTableName();
     $builder->addSelect("{$table}.{$this->joinStorageKey}");
     $builder->addSelect("COUNT({$table}.{$targetStoragePrimaryKey}) AS count");
     $builder->addWhere("{$table}.{$this->joinStorageKey}", $values);
     $builder->setGroup("{$table}.{$this->joinStorageKey}");
     $builder->setOrder([], []);
     $result = $this->context->queryArgs($builder->buildSelectQuery(), $builder->getParameters());
     $counts = [];
     foreach ($result->fetchAll() as $row) {
         $counts[$row->{$this->joinStorageKey}] = $row['count'];
     }
     return $counts;
 }
Example #7
0
 /**
  * @param  string $condition
  * @param  string $direction
  * @return Closure
  */
 public function createSorter(array $conditions)
 {
     $columns = [];
     foreach ($conditions as $pair) {
         list($column, , $sourceEntity) = ConditionParserHelper::parseCondition($pair[0]);
         $sourceEntityMeta = $this->metadataStorage->get($sourceEntity ?: $this->mapper->getRepository()->getEntityClassNames()[0]);
         $columns[] = [$column, $pair[1], $sourceEntityMeta];
     }
     return function ($a, $b) use($columns) {
         foreach ($columns as $pair) {
             $_a = $this->getter($a, $pair[0], $pair[2]);
             $_b = $this->getter($b, $pair[0], $pair[2]);
             $direction = $pair[1] === ICollection::ASC ? 1 : -1;
             if (is_int($_a) || is_float($_a)) {
                 if ($_a < $_b) {
                     return $direction * -1;
                 } elseif ($_a > $_b) {
                     return $direction;
                 }
             } elseif ($_a instanceof \Datetime) {
                 if ($_a < $_b) {
                     return $direction * -1;
                 } elseif ($_b > $_a) {
                     return $direction;
                 }
             } else {
                 $res = strcmp((string) $_a, (string) $_b);
                 if ($res < 0) {
                     return $direction * -1;
                 } elseif ($res > 0) {
                     return $direction;
                 }
             }
         }
         return 0;
     };
 }
 public function __construct(Connection $connection, IMapper $targetMapper, PropertyMetadata $metadata)
 {
     $this->connection = $connection;
     $this->targetRepository = $targetMapper->getRepository();
     $this->metadata = $metadata;
 }
 public function __construct(IMapper $targetMapper, PropertyMetadata $metadata)
 {
     $this->metadata = $metadata;
     $this->joinStorageKey = $targetMapper->getStorageReflection()->convertEntityToStorageKey($this->metadata->relationship->property);
 }
Example #10
0
 public function getManyHasManyStoragePrimaryKeys(IMapper $target)
 {
     $one = $this->getStoragePrimaryKey()[0];
     $two = $target->getStorageReflection()->getStoragePrimaryKey()[0];
     if ($one !== $two) {
         return [$one, $two];
     }
     return $this->findManyHasManyPrimaryColumns($this->getManyHasManyStorageName($target), $this->getStorageName(), $target->getTableName());
 }
Example #11
0
 protected function prefix(string $string) : string
 {
     return implode('.', [str_replace('_', '.', $this->mapper->getTableName()), $string]);
 }
Example #12
0
 public function setRepository(IRepository $repository)
 {
     parent::setRepository($repository);
     $this->originMapper->setRepository($repository);
 }