Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #522 from jaugustin/fix-select-withColumn-order

fix wrong order of result when using withColumn and select(),
  • Loading branch information...
commit 918cd5f7ac600c9c087c8798bd165b212ab80648 2 parents 0dededa + 130f941
@willdurand willdurand authored
View
7 runtime/lib/query/ModelCriteria.php
@@ -560,12 +560,17 @@ protected function configureSelectColumns()
// Add requested columns which are not withColumns
$columnNames = is_array($this->select) ? $this->select : array($this->select);
+ // temporary store columns Alias or withColumn
+ $asColumns = $this->getAsColumns();
+ $this->asColumns = array();
foreach ($columnNames as $columnName) {
// check if the column was added by a withColumn, if not add it
- if (!array_key_exists($columnName, $this->getAsColumns())) {
+ if (!array_key_exists($columnName, $asColumns)) {
$column = $this->getColumnFromName($columnName);
// always put quotes around the columnName to be safe, we strip them in the formatter
$this->addAsColumn('"' . $columnName . '"', $column[1]);
+ } else {
+ $this->addAsColumn($columnName, $asColumns[$columnName]);
}
}
}
View
44 test/testsuite/runtime/query/ModelCriteriaSelectTest.php
@@ -363,6 +363,50 @@ public function testSelectArrayWithColumn()
$this->assertEquals(serialize($rows->getData()), serialize($expectedRows), 'find() called after select(array) can cope with a column added with withColumn()');
}
+ public function testSelectArrayWithColumnOrder()
+ {
+ BookstoreDataPopulator::depopulate($this->con);
+ BookstoreDataPopulator::populate($this->con);
+
+ $c = new ModelCriteria('bookstore', 'Book');
+ $c->join('Book.Author');
+ $c->withColumn('LOWER(Book.Title)', 'LowercaseTitle');
+ $c->withColumn('UPPER(Book.Title)', 'UppercaseTitle');
+ $c->select(array('Book.ISBN', 'LowercaseTitle', 'Book.Title', 'UppercaseTitle'));
+ $c->orderBy('Book.Title');
+ $rows = $c->find($this->con);
+ $expectedSQL = 'SELECT book.isbn AS "Book.ISBN", LOWER(book.title) AS LowercaseTitle, book.title AS "Book.Title", UPPER(book.title) AS UppercaseTitle FROM `book` INNER JOIN `author` ON (book.author_id=author.id) ORDER BY book.title ASC';
+ $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'find() called after select(array) can cope with a column added with withColumn()');
+
+ $expectedRows = array (
+ array (
+ 'Book.ISBN' => '0140422161',
+ 'LowercaseTitle' => 'don juan',
+ 'Book.Title' => 'Don Juan',
+ 'UppercaseTitle' => 'DON JUAN',
+ ),
+ array (
+ 'Book.ISBN' => '043935806X',
+ 'LowercaseTitle' => 'harry potter and the order of the phoenix',
+ 'Book.Title' => 'Harry Potter and the Order of the Phoenix',
+ 'UppercaseTitle' => 'HARRY POTTER AND THE ORDER OF THE PHOENIX',
+ ),
+ array (
+ 'Book.ISBN' => '0380977427',
+ 'LowercaseTitle' => 'quicksilver',
+ 'Book.Title' => 'Quicksilver',
+ 'UppercaseTitle' => 'QUICKSILVER',
+ ),
+ array (
+ 'Book.ISBN' => '067972575X',
+ 'LowercaseTitle' => 'the tin drum',
+ 'Book.Title' => 'The Tin Drum',
+ 'UppercaseTitle' => 'THE TIN DRUM',
+ ),
+ );
+ $this->assertEquals(serialize($rows->getData()), serialize($expectedRows), 'find() called after select(array) can cope with a column added with withColumn()');
+ }
+
public function testSelectArrayPaginate()
{
BookstoreDataPopulator::depopulate($this->con);
Please sign in to comment.
Something went wrong with that request. Please try again.