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

Fix issue #227 : Small change in ModelCriteria, to make it work with peer constants #279

Merged
merged 1 commit into from Feb 5, 2012
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 18 additions & 13 deletions runtime/lib/query/ModelCriteria.php
Expand Up @@ -1918,41 +1918,46 @@ protected function doReplaceNameInExpression($matches)
protected function getColumnFromName($phpName, $failSilently = true)
{
if (strpos($phpName, '.') === false) {
$class = $this->getModelAliasOrName();
$prefix = $this->getModelAliasOrName();
} else {
list($class, $phpName) = explode('.', $phpName);
// $prefix could be either class name or table name
list($prefix, $phpName) = explode('.', $phpName);
}

if ($class == $this->getModelAliasOrName()) {
// column of the Criteria's model
if ($prefix == $this->getModelAliasOrName() || $prefix == $this->getTableMap()->getName()) {
// column of the Criteria's model, or column name from Criteria's peer
$tableMap = $this->getTableMap();
} elseif (isset($this->joins[$class])) {
} elseif (isset($this->joins[$prefix])) {
// column of a relations's model
$tableMap = $this->joins[$class]->getTableMap();
} elseif ($this->hasSelectQuery($class)) {
return $this->getColumnFromSubQuery($class, $phpName, $failSilently);
$tableMap = $this->joins[$prefix]->getTableMap();
} elseif ($this->hasSelectQuery($prefix)) {
return $this->getColumnFromSubQuery($prefix, $phpName, $failSilently);
} elseif ($failSilently) {
return array(null, null);
} else {
throw new PropelException(sprintf('Unknown model or alias "%s"', $class));
throw new PropelException(sprintf('Unknown model, alias or table "%s"', $prefix));
}

if ($tableMap->hasColumnByPhpName($phpName)) {
$column = $tableMap->getColumnByPhpName($phpName);
if (isset($this->aliases[$class])) {
$this->currentAlias = $class;
$realColumnName = $class . '.' . $column->getName();
if (isset($this->aliases[$prefix])) {
$this->currentAlias = $prefix;
$realColumnName = $prefix . '.' . $column->getName();
} else {
$realColumnName = $column->getFullyQualifiedName();
}
return array($column, $realColumnName);
} elseif ($tableMap->hasColumn($phpName,false)) {
$column = $tableMap->getColumn($phpName,false);
$realColumnName = $column->getFullyQualifiedName();
return array($column, $realColumnName);
} elseif (isset($this->asColumns[$phpName])) {
// aliased column
return array(null, $phpName);
} elseif ($failSilently) {
return array(null, null);
} else {
throw new PropelException(sprintf('Unknown column "%s" on model or alias "%s"', $phpName, $class));
throw new PropelException(sprintf('Unknown column "%s" on model, alias or table "%s"', $phpName, $prefix));
}
}

Expand Down
21 changes: 20 additions & 1 deletion test/testsuite/runtime/query/ModelCriteriaSelectTest.php
Expand Up @@ -98,6 +98,25 @@ public function testSelectStringFind()
$this->assertEquals($authors->count(), 1, 'find() called after select(string) allows for where() statements');
$expectedSQL = "SELECT author.FIRST_NAME AS \"FirstName\" FROM `author` WHERE author.FIRST_NAME = 'Neal'";
$this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'find() called after select(string) allows for where() statements');

$c = new ModelCriteria('bookstore', 'Author');
$c->select(AuthorPeer::FIRST_NAME);
$author = $c->find($this->con);
$expectedSQL = "SELECT author.FIRST_NAME AS \"author.FIRST_NAME\" FROM `author`";
$this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'select(string) accepts model Peer Constants');
}

/**
* @expectedException PropelException
*/
public function testSelectStringFindCalledWithNonExistingColumn()
{
BookstoreDataPopulator::depopulate($this->con);
BookstoreDataPopulator::populate($this->con);

$c = new ModelCriteria('bookstore', 'Author');
$c->select('author.NOT_EXISTING_COLUMN');
$author = $c->find($this->con);
}

public function testSelectStringFindOne()
Expand All @@ -121,7 +140,7 @@ public function testSelectStringFindOne()
$expectedSQL = "SELECT author.FIRST_NAME AS \"FirstName\" FROM `author` WHERE author.FIRST_NAME = 'Neal' LIMIT 1";
$this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'findOne() called after select(string) allows for where() statements');
}

public function testSelectStringJoin()
{
BookstoreDataPopulator::depopulate($this->con);
Expand Down