From 2f9c987eb185b93483bf54019b592861da79578a Mon Sep 17 00:00:00 2001 From: Bora Tunca Date: Sun, 5 Feb 2012 17:27:40 +0100 Subject: [PATCH] fixed and tested --- runtime/lib/query/ModelCriteria.php | 31 +++++++++++-------- .../runtime/query/ModelCriteriaSelectTest.php | 21 ++++++++++++- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/runtime/lib/query/ModelCriteria.php b/runtime/lib/query/ModelCriteria.php index 99e6719ef..c97880143 100644 --- a/runtime/lib/query/ModelCriteria.php +++ b/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)); } } diff --git a/test/testsuite/runtime/query/ModelCriteriaSelectTest.php b/test/testsuite/runtime/query/ModelCriteriaSelectTest.php index 2d3280b51..b111ea269 100644 --- a/test/testsuite/runtime/query/ModelCriteriaSelectTest.php +++ b/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);