diff --git a/lib/Doctrine/Query.php b/lib/Doctrine/Query.php index f2123a4f5..570e255a0 100644 --- a/lib/Doctrine/Query.php +++ b/lib/Doctrine/Query.php @@ -1,5 +1,5 @@ $val) { diff --git a/lib/Doctrine/Query/Where.php b/lib/Doctrine/Query/Where.php index 8a92d1ea8..f03f71a6a 100644 --- a/lib/Doctrine/Query/Where.php +++ b/lib/Doctrine/Query/Where.php @@ -12,17 +12,20 @@ class Doctrine_Query_Where extends Doctrine_Query_Condition { public function load($where) { $e = Doctrine_Query::sqlExplode($where); + + if(count($e) < 3) { + $e = Doctrine_Query::sqlExplode($where, array('=', '<', '>', '!=')); + } $r = array_shift($e); + $a = explode(".",$r); if(count($a) > 1) { $field = array_pop($a); $count = count($e); - $slice = array_slice($e, 0, ($count - 1)); - $operator = implode(' ', $slice); - $slice = array_slice($e, -1, 1); $value = implode('', $slice); + $operator = trim(substr($where, strlen($r), -strlen($value))); $reference = implode(".",$a); $count = count($a); @@ -99,7 +102,8 @@ class Doctrine_Query_Where extends Doctrine_Query_Condition { $where = $this->query->getTableAlias($reference).'.'.$field.' '.$operator.' '.$value; } } - } + } + return $where; } diff --git a/tests/QueryWhereTestCase.php b/tests/QueryWhereTestCase.php index 7d0eb7890..7ea1c5a00 100644 --- a/tests/QueryWhereTestCase.php +++ b/tests/QueryWhereTestCase.php @@ -5,7 +5,7 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase { $this->tables = array('entity'); parent::prepareTables(); } - public function testQueryWithDirectParameterSetting() { + public function testDirectParameterSetting() { $this->connection->clear(); $user = new User(); @@ -13,28 +13,68 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase { $user->save(); $q = new Doctrine_Query(); - + $q->from('User(id)')->addWhere('User.id = ?',1); $users = $q->execute(); - + $this->assertEqual($users->count(), 1); $this->assertEqual($users[0]->name, 'someone'); } - public function testQueryWithDirectMultipleParameterSetting() { + public function testDirectMultipleParameterSetting() { $user = new User(); - $user->name = 'someone 2'; + $user->name = 'someone.2'; $user->save(); $q = new Doctrine_Query(); - + $q->from('User(id)')->addWhere('User.id IN (?, ?)',array(1,2)); $users = $q->execute(); - + $this->assertEqual($users->count(), 2); $this->assertEqual($users[0]->name, 'someone'); - $this->assertEqual($users[1]->name, 'someone 2'); + $this->assertEqual($users[1]->name, 'someone.2'); + } + public function testOperatorWithNoTrailingSpaces() { + $q = new Doctrine_Query(); + + $q->from('User(id)')->where("User.name='someone'"); + + $users = $q->execute(); + $this->assertEqual($users->count(), 1); + + $this->assertEqual($q->getQuery(), "SELECT entity.id AS entity__id FROM entity WHERE entity.name = 'someone' AND (entity.type = 0)"); + } + public function testOperatorWithNoTrailingSpaces2() { + $q = new Doctrine_Query(); + + $q->from('User(id)')->where("User.name='foo.bar'"); + + $users = $q->execute(); + $this->assertEqual($users->count(), 0); + + $this->assertEqual($q->getQuery(), "SELECT entity.id AS entity__id FROM entity WHERE entity.name = 'foo.bar' AND (entity.type = 0)"); + } + public function testOperatorWithSingleTrailingSpace() { + $q = new Doctrine_Query(); + + $q->from('User(id)')->where("User.name= 'foo.bar'"); + + $users = $q->execute(); + $this->assertEqual($users->count(), 0); + + $this->assertEqual($q->getQuery(), "SELECT entity.id AS entity__id FROM entity WHERE entity.name = 'foo.bar' AND (entity.type = 0)"); + } + public function testOperatorWithSingleTrailingSpace2() { + $q = new Doctrine_Query(); + + $q->from('User(id)')->where("User.name ='foo.bar'"); + + $users = $q->execute(); + $this->assertEqual($users->count(), 0); + + $this->assertEqual($q->getQuery(), "SELECT entity.id AS entity__id FROM entity WHERE entity.name = 'foo.bar' AND (entity.type = 0)"); } } ?>