Skip to content
Browse files

fixed and tested

  • Loading branch information...
1 parent 5850dce commit 2f9c987eb185b93483bf54019b592861da79578a @grandbora grandbora committed Feb 5, 2012
Showing with 38 additions and 14 deletions.
  1. +18 −13 runtime/lib/query/ModelCriteria.php
  2. +20 −1 test/testsuite/runtime/query/ModelCriteriaSelectTest.php
View
31 runtime/lib/query/ModelCriteria.php
@@ -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));
}
}
View
21 test/testsuite/runtime/query/ModelCriteriaSelectTest.php
@@ -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()
@@ -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);

0 comments on commit 2f9c987

Please sign in to comment.
Something went wrong with that request. Please try again.