diff --git a/Tests/dblayerooComplet.php b/Tests/dblayerooComplet.php index 945c9eb..c908212 100644 --- a/Tests/dblayerooComplet.php +++ b/Tests/dblayerooComplet.php @@ -744,6 +744,19 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase $db1->sep()."group".$db1->sep().")"), $res); } + public function test_displayAdd_NotFull5 () + { + // With alias + $db1 = $this->db1 (); + $db1->displayAdd ("group_concat (distinct group ) ", "Group Alias"); + $res = $db1->displayGet (); + $db1->disconnect (); + $this->assertSame (array ( + "order1" => "GROUP_CONCAT(DISTINCT ". + $db1->sep()."group".$db1->sep().") AS ". + $db1->sep()."Group Alias".$db1->sep()), $res); + } + public function test_displayAdd_Full1 () { $db1 = $this->db1 (); @@ -790,6 +803,20 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase $db1->sep()."group".$db1->sep().")"), $res); } + public function test_displayAdd_Full5 () + { + // With alias + $db1 = $this->db1 (); + $db1->displayAdd ("group_concat (distinct group ) ", "Group Alias"); + $res = $db1->displayGet (true); + $db1->disconnect (); + $this->assertSame (array ( + "order1" => "GROUP_CONCAT(DISTINCT ". + $db1->sep()."groupedoo".$db1->sep().".". + $db1->sep()."group".$db1->sep().") AS ". + $db1->sep()."Group Alias".$db1->sep()), $res); + } + public function test_displayAdd_Multiple1 () { $db1 = $this->db1 (); diff --git a/dblayeroo.php b/dblayeroo.php index 0af657c..e6915b1 100644 --- a/dblayeroo.php +++ b/dblayeroo.php @@ -1341,6 +1341,9 @@ class dblayeroo * correctely defined */ private $displayColumn = null; + /** The alias associated to each displayColumn + */ + private $displayAlias = array (); /** Manage the joins */ private $joins = array (); @@ -1410,6 +1413,7 @@ class dblayeroo $this->command = ""; $this->distinct = ""; $this->displayColumn = null; + $this->displayAlias = array (); $this->joins = array (); $this->whereExpression = array (); $this->whereValues = array (); @@ -1532,19 +1536,30 @@ class dblayeroo * By default, display all the columns if this method is not called * If the value is null or not provided or an empty array, do not display * any field + * @param array|string|null $aliasNames Add the Aliases to the displayed + * columns */ - public function displayAdd ($columnNames = array ()) + public function displayAdd ($columnNames = array (), $aliasNames = array ()) /* {{{ */ { - $this->debugLog ("Entering displayAdd (",$columnNames,")"); + $this->debugLog ("Entering displayAdd (", $columnNames, ",", $aliasNames, + ")"); if (! is_string ($columnNames) && ! is_array ($columnNames)) $this->DBException ( "Invalid columnNames provided (not string and not array)"); + if (! is_string ($aliasNames) && ! is_array ($aliasNames)) + $this->DBException ( + "Invalid aliasNames provided (not string and not array)"); if (is_string ($columnNames)) $columnNames = explode (",", $columnNames); + if (is_string ($aliasNames)) + $aliasNames = explode (",", $aliasNames); + if (count ($aliasNames) && count ($aliasNames) !== count ($columnNames)) + $this->DBException ( + "The number of aliasNames are not the same as the number of columns"); if ($this->displayColumn === null) $this->displayColumn = array (); - foreach ($columnNames as $display) + foreach ($columnNames as $nb => $display) { $display = $name = trim ($display); $pos = strpos ($display, "("); @@ -1602,6 +1617,8 @@ class dblayeroo } unset ($aggregateFunction); unset ($func); + if (key_exists ($nb, $aliasNames)) + $this->displayAlias[$getSortOrder] = $aliasNames[$nb]; } return $this; } @@ -1705,6 +1722,9 @@ class dblayeroo $columns[$pos] = $this->displayConvert ($name, true); else $columns[$pos] = $this->displayConvert ($name); + if (key_exists ($pos, $this->displayAlias)) + $columns[$pos] .= " AS ". + $this->sep.$this->displayAlias[$pos].$this->sep; } if ($this->joinObject) {