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}"; }
public function getter($element, $chain, EntityMetadata $sourceEntityMeta) { $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 $this->normalizeValue($value, $propertyMeta); } else { $targetEntityMeta = $this->metadataStorage->get($propertyMeta->relationship->entity); return $this->getter($value, $chain, $targetEntityMeta); } }
/** * @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; }; }
protected function createMetadata() { return MetadataStorage::get(get_class($this)); }
/** @inheritdoc */ public function getEntityMetadata() { return $this->metadataStorage->get(static::getEntityClassNames()[0]); }
private function normalizeAndAddJoins(array $levels, $sourceEntity, QueryBuilder $builder, &$distinctNeeded = false, &$value = null, &$modifier = '%any') { $column = array_pop($levels); $sourceMapper = $this->mapper; $sourceAlias = $builder->getFromAlias(); $sourceReflection = $sourceMapper->getStorageReflection(); $sourceEntityMeta = $this->metadataStorage->get($sourceEntity ?: $sourceMapper->getRepository()->getEntityClassNames()[0]); foreach ($levels as $levelIndex => $level) { $property = $sourceEntityMeta->getProperty($level); if ($property->relationship === null) { throw new InvalidArgumentException("Entity {$sourceEntityMeta->className}::\${$level} does not contain a relationship."); } $targetMapper = $this->model->getRepository($property->relationship->repository)->getMapper(); $targetReflection = $targetMapper->getStorageReflection(); $targetEntityMetadata = $this->metadataStorage->get($property->relationship->entity); $relType = $property->relationship->type; if ($relType === Relationship::ONE_HAS_MANY || $relType === Relationship::ONE_HAS_ONE && !$property->relationship->isMain) { $targetColumn = $targetReflection->convertEntityToStorageKey($property->relationship->property); $sourceColumn = $sourceReflection->getStoragePrimaryKey()[0]; $distinctNeeded = $relType === Relationship::ONE_HAS_MANY; } elseif ($relType === Relationship::MANY_HAS_MANY) { if ($property->relationship->isMain) { list($joinTable, list($inColumn, $outColumn)) = $sourceMapper->getManyHasManyParameters($property, $targetMapper); } else { $sourceProperty = $targetEntityMetadata->getProperty($property->relationship->property); list($joinTable, list($outColumn, $inColumn)) = $targetMapper->getManyHasManyParameters($sourceProperty, $sourceMapper); } $sourceColumn = $sourceReflection->getStoragePrimaryKey()[0]; $builder->leftJoin($sourceAlias, $joinTable, self::getAlias($joinTable), "[{$sourceAlias}.{$sourceColumn}] = [{$joinTable}.{$inColumn}]"); $sourceAlias = $joinTable; $sourceColumn = $outColumn; $targetColumn = $targetReflection->getStoragePrimaryKey()[0]; $distinctNeeded = true; } else { $targetColumn = $targetReflection->getStoragePrimaryKey()[0]; $sourceColumn = $sourceReflection->convertEntityToStorageKey($level); } $targetTable = $targetMapper->getTableName(); $targetAlias = $level . str_repeat('_', $levelIndex); $builder->leftJoin($sourceAlias, $targetTable, $targetAlias, "[{$sourceAlias}.{$sourceColumn}] = [{$targetAlias}.{$targetColumn}]"); $sourceAlias = $targetAlias; $sourceMapper = $targetMapper; $sourceReflection = $targetReflection; $sourceEntityMeta = $targetEntityMetadata; } $targetProperty = $sourceEntityMeta->getProperty($column); if ($targetProperty->isPrimary && $targetProperty->isVirtual) { // primary-proxy $primaryKey = $sourceEntityMeta->getPrimaryKey(); if (count($primaryKey) > 1) { // composite primary key $modifier = '%any'; list($expression, $value) = $this->processMultiColumn($sourceReflection, $primaryKey, $value, $sourceAlias); return $expression; } else { $column = reset($primaryKey); } } list($expression, $modifier, $value) = $this->processColumn($sourceReflection, $column, $value, $sourceAlias); return $expression; }
private function normalizeAndAddJoins(array $levels, $sourceEntity, QueryBuilder $builder, &$distinctNeeded = FALSE, &$value = NULL) { $column = array_pop($levels); $sourceMapper = $this->mapper; $sourceAlias = $builder->getFromAlias(); $sourceReflection = $sourceMapper->getStorageReflection(); $sourceEntityMeta = $this->metadataStorage->get($sourceEntity ?: $sourceMapper->getRepository()->getEntityClassNames()[0]); foreach ($levels as $levelIndex => $level) { $property = $sourceEntityMeta->getProperty($level); if ($property->relationship === NULL) { throw new InvalidArgumentException("Entity {$sourceEntityMeta->className}::\${$level} does not contain a relationship."); } $targetMapper = $this->model->getRepository($property->relationship->repository)->getMapper(); $targetReflection = $targetMapper->getStorageReflection(); $targetEntityMetadata = $this->metadataStorage->get($property->relationship->entity); $relType = $property->relationship->type; if ($relType === Relationship::ONE_HAS_MANY || $relType === Relationship::ONE_HAS_ONE_DIRECTED && !$property->relationship->isMain) { $targetColumn = $targetReflection->convertEntityToStorageKey($property->relationship->property); $sourceColumn = $sourceReflection->getStoragePrimaryKey()[0]; $distinctNeeded = TRUE; } elseif ($relType === Relationship::MANY_HAS_MANY) { if ($property->relationship->isMain) { list($joinTable, list($inColumn, $outColumn)) = $sourceMapper->getManyHasManyParameters($property, $targetMapper); } else { $sourceProperty = $targetEntityMetadata->getProperty($property->relationship->property); list($joinTable, list($outColumn, $inColumn)) = $targetMapper->getManyHasManyParameters($sourceProperty, $sourceMapper); } $sourceColumn = $sourceReflection->getStoragePrimaryKey()[0]; $builder->leftJoin($sourceAlias, $joinTable, self::getAlias($joinTable), "[{$sourceAlias}.{$sourceColumn}] = [{$joinTable}.{$inColumn}]"); $sourceAlias = $joinTable; $sourceColumn = $outColumn; $targetColumn = $targetReflection->getStoragePrimaryKey()[0]; $distinctNeeded = TRUE; } else { $targetColumn = $targetReflection->getStoragePrimaryKey()[0]; $sourceColumn = $sourceReflection->convertEntityToStorageKey($level); } $targetTable = $targetMapper->getTableName(); $targetAlias = $level . str_repeat('_', $levelIndex); $builder->leftJoin($sourceAlias, $targetTable, $targetAlias, "[{$sourceAlias}.{$sourceColumn}] = [{$targetAlias}.{$targetColumn}]"); $sourceAlias = $targetAlias; $sourceMapper = $targetMapper; $sourceReflection = $targetReflection; $sourceEntityMeta = $targetEntityMetadata; } $sourceEntityMeta->getProperty($column); // check if property exists if ($column === 'id' && count($sourceEntityMeta->getPrimaryKey()) > 1) { $pair = []; foreach ($sourceEntityMeta->getPrimaryKey() as $column) { $column = $sourceReflection->convertEntityToStorageKey($column); $pair[] = "{$sourceAlias}.{$column}"; } if (!isset($value[0][0])) { $value = [$value]; } return '(' . implode(', ', $pair) . ')'; } else { $column = $sourceReflection->convertEntityToStorageKey($column); return "{$sourceAlias}.{$column}"; } }
/** * @param string $class * @return EntityMetadata */ public function getEntityMetadata($class) { if (!isset($this->entityMetadata[$class])) { $this->entityMetadata[$class] = MetadataStorage::get($class); } return $this->entityMetadata[$class]; }