From c480e19b6c3e6189eded303deb2fdc7da488a644 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 5 Jul 2011 09:49:38 -0700 Subject: [PATCH] Added better support for aliases column to Query class. --- system/db/query.php | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/system/db/query.php b/system/db/query.php index 85e02b2f..5f0a0bd3 100644 --- a/system/db/query.php +++ b/system/db/query.php @@ -120,8 +120,41 @@ class Query { public function select() { $this->select = ($this->distinct) ? 'SELECT DISTINCT ' : 'SELECT '; - $this->select .= implode(', ', array_map(array($this, 'wrap'), func_get_args())); + $columns = array(); + + foreach (func_get_args() as $column) + { + // --------------------------------------------------------- + // If the column name is being aliases, we will need to + // wrap the column name and its alias. + // --------------------------------------------------------- + if (strpos(strtolower($column), ' as ') !== false) + { + $segments = explode(' ', $column); + + $columns[] = $this->wrap($segments[0]).' AS '.$this->wrap($segments[2]); + } + else + { + $columns[] = $this->wrap($column); + } + } + + $this->select .= implode(', ', $columns); + + return $this; + } + + /** + * Set the FROM clause. + * + * @param string $from + * @return Query + */ + public function from($from) + { + $this->from = $from; return $this; }