Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

select() + withColumn() query error #611

Merged
merged 2 commits into from
Feb 16, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion runtime/lib/query/ModelCriteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,8 @@ protected function configureSelectColumns()
}

// Add requested columns which are not withColumns
$columnNames = is_array($this->select) ? $this->select : array($this->select);
$columnNames = (array) $this->select;

// temporary store columns Alias or withColumn
$asColumns = $this->getAsColumns();
$this->asColumns = array();
Expand All @@ -574,8 +575,15 @@ protected function configureSelectColumns()
$this->addAsColumn('"' . $columnName . '"', $column[1]);
} else {
$this->addAsColumn($columnName, $asColumns[$columnName]);
// remove already added columns
unset($asColumns[$columnName]);
}
}

// re-add all remaining columns which were not contained in select-columnNames
foreach($asColumns as $columnName => $columnAlias) {
$this->addAsColumn($columnName, $columnAlias);
}
}

/**
Expand Down
19 changes: 18 additions & 1 deletion test/testsuite/runtime/query/ModelCriteriaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
class ModelCriteriaTest extends BookstoreTestBase
{
protected function assertCriteriaTranslation($criteria, $expectedSql, $expectedParams, $message = '')
protected function assertCriteriaTranslation(Criteria $criteria, $expectedSql, $expectedParams, $message = '')
{
$params = array();
$result = BasePeer::createSelectSql($criteria, $params);
Expand Down Expand Up @@ -1319,6 +1319,23 @@ public function testWithColumnAndQuotes($clause, $alias, $selectTranslation)
$this->assertCriteriaTranslation($c, $sql, $params, 'withColumn() adds a calculated column using quotes to the select clause');
}

public function testWithColumnAndSelect()
{
$c = new ModelCriteria('bookstore', 'Author');
$c->join('Book');
$c->withColumn('COUNT(Book.Id)', 'NbBooks');
$c->select(array('FirstName', 'LastName'));
$collection = $c->find();

$this->assertThat($collection, $this->isInstanceOf('PropelCollection'));

foreach($collection as $array) {
$this->assertArrayHasKey('FirstName', $array);
$this->assertArrayHasKey('LastName', $array);
$this->assertArrayHasKey('NbBooks', $array);
}
}

public function testWithColumnAndSelectColumns()
{
$c = new ModelCriteria('bookstore', 'Book');
Expand Down