/** * Resolve the given columns supposed to be fetched * * This notifies the repository about each desired query column. * * @param mixed $target The target where to look for each column * @param array $desiredColumns Pass null or an empty array to require all query columns * * @return array The desired columns indexed by their respective alias */ protected function prepareQueryColumns($target, array $desiredColumns = null) { $this->customAliases = array(); if (empty($desiredColumns)) { $columns = $this->repository->requireAllQueryColumns($target); } else { $columns = array(); foreach ($desiredColumns as $customAlias => $columnAlias) { $resolvedColumn = $this->repository->requireQueryColumn($target, $columnAlias, $this); if ($resolvedColumn !== $columnAlias) { if (is_string($customAlias)) { $columns[$customAlias] = $resolvedColumn; $this->customAliases[$customAlias] = $columnAlias; } else { $columns[$columnAlias] = $resolvedColumn; } } elseif (is_string($customAlias)) { $columns[$customAlias] = $columnAlias; $this->customAliases[$customAlias] = $columnAlias; } else { $columns[] = $columnAlias; } } } return $columns; }
/** * Validate that the given column is a valid query target and return it or the actual name if it's an alias * * Attempts to join the given column from a different table if its association to the given table cannot be * verified. * * @param string $table The table where to look for the column or alias * @param string $name The name or alias of the column to validate * @param RepositoryQuery $query An optional query to pass as context, * if not given no join will be attempted * * @return string The given column's name * * @throws QueryException In case the given column is not a valid query column * @throws ProgrammingError In case the given column is not found in $table and cannot be joined in */ public function requireQueryColumn($table, $name, RepositoryQuery $query = null) { if ($name instanceof Zend_Db_Expr) { return $name; } if ($query === null || $this->validateQueryColumnAssociation($table, $name)) { return parent::requireQueryColumn($table, $name, $query); } $column = $this->joinColumn($name, $table, $query); if ($column === null) { if ($query !== null) { // It may be an aliased Zend_Db_Expr $desiredColumns = $query->getColumns(); if (isset($desiredColumns[$name]) && $desiredColumns[$name] instanceof Zend_Db_Expr) { $column = $desiredColumns[$name]; } } if ($column === null) { throw new ProgrammingError('Unable to find a valid table for column "%s" to join into "%s"', $name, $table); } } return $column; }
/** * Validate that the given column is a valid query target and return it or the actual name if it's an alias * * Attempts to join the given column from a different table if its association to the given table cannot be * verified. * * @param string $table The table where to look for the column or alias * @param string $name The name or alias of the column to validate * @param RepositoryQuery $query An optional query to pass as context, * if not given no join will be attempted * * @return string The given column's name * * @throws QueryException In case the given column is not a valid query column */ public function requireQueryColumn($table, $name, RepositoryQuery $query = null) { if ($query === null || $this->validateQueryColumnAssociation($table, $name)) { return parent::requireQueryColumn($table, $name, $query); } return $this->joinColumn($name, $table, $query); }