Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #138 from pylebecq/master

Fixed ModelCriteria::replaceNames() to handle namespaced model classes
  • Loading branch information...
commit ce5b28fca717fe45c878167c8594a2b97da8ebad 2 parents 3a7aa11 + a20ad17
@willdurand willdurand authored
View
4 runtime/lib/query/ModelCriteria.php
@@ -1840,7 +1840,7 @@ protected function replaceNames(&$clause)
$isInString = false;
}
} elseif (!$isInString) {
- $parsedString .= preg_replace_callback('/\w+\.\w+/', array($this, 'doReplaceNameInExpression'), $stringToTransform);
+ $parsedString .= preg_replace_callback("/[\w\\\]+\.\w+/", array($this, 'doReplaceNameInExpression'), $stringToTransform);
$stringToTransform = '';
$stringQuotes = $char;
$isInString = true;
@@ -1858,7 +1858,7 @@ protected function replaceNames(&$clause)
$pos++;
}
if ($stringToTransform) {
- $parsedString .= preg_replace_callback('/\w+\.\w+/', array($this, 'doReplaceNameInExpression'), $stringToTransform);
+ $parsedString .= preg_replace_callback("/[\w\\\]+\.\w+/", array($this, 'doReplaceNameInExpression'), $stringToTransform);
}
$clause = $parsedString;
View
66 test/testsuite/runtime/query/ModelCriteriaWithNamespaceTest.php
@@ -0,0 +1,66 @@
+<?php
+
+/**
+ * This file is part of the Propel package.
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @license MIT License
+ */
+
+require_once dirname(__FILE__) . '/../../../tools/helpers/namespaces/NamespacesTestBase.php';
+
+/**
+ * Test class for ModelCriteria with namespaces.
+ *
+ * @author Pierre-Yves LEBECQ <py.lebecq@gmail.com>
+ * @package runtime.query
+ */
+class ModelCriteriaWithNamespaceTest extends NamespacesTestBase
+{
+ public static function conditionsForTestReplaceNamesWithNamespaces()
+ {
+ return array(
+ array('Foo\\Bar\\NamespacedBook.Title = ?', 'Title', 'book.TITLE = ?'), // basic case
+ array('Foo\\Bar\\NamespacedBook.Title=?', 'Title', 'book.TITLE=?'), // without spaces
+ array('Foo\\Bar\\NamespacedBook.Id<= ?', 'Id', 'book.ID<= ?'), // with non-equal comparator
+ array('Foo\\Bar\\NamespacedBook.AuthorId LIKE ?', 'AuthorId', 'book.AUTHOR_ID LIKE ?'), // with SQL keyword separator
+ array('(Foo\\Bar\\NamespacedBook.AuthorId) LIKE ?', 'AuthorId', '(book.AUTHOR_ID) LIKE ?'), // with parenthesis
+ array('(Foo\\Bar\\NamespacedBook.Id*1.5)=1', 'Id', '(book.ID*1.5)=1'), // ignore numbers
+ // dealing with quotes
+ array("Foo\\Bar\\NamespacedBook.Id + ' ' + Foo\\Bar\\NamespacedBook.AuthorId", null, "book.ID + ' ' + book.AUTHOR_ID"),
+ array("'Foo\\Bar\\NamespacedBook.Id' + Foo\\Bar\\NamespacedBook.AuthorId", null, "'Foo\\Bar\\NamespacedBook.Id' + book.AUTHOR_ID"),
+ array("Foo\\Bar\\NamespacedBook.Id + 'Foo\\Bar\\NamespacedBook.AuthorId'", null, "book.ID + 'Foo\\Bar\\NamespacedBook.AuthorId'"),
+ );
+ }
+
+ /**
+ * @dataProvider conditionsForTestReplaceNamesWithNamespaces
+ */
+ public function testReplaceNamesWithNamespaces($origClause, $columnPhpName = false, $modifiedClause)
+ {
+ $c = new TestableModelCriteriaWithNamespace('bookstore_namespaced', 'Foo\\Bar\\NamespacedBook');
+ $this->doTestReplaceNames($c, Foo\Bar\NamespacedBookPeer::getTableMap(), $origClause, $columnPhpName = false, $modifiedClause);
+ }
+
+ public function doTestReplaceNames($c, $tableMap, $origClause, $columnPhpName = false, $modifiedClause)
+ {
+ $c->replaceNames($origClause);
+ $columns = $c->replacedColumns;
+ if ($columnPhpName) {
+ $this->assertEquals(array($tableMap->getColumnByPhpName($columnPhpName)), $columns);
+ }
+ $this->assertEquals($modifiedClause, $origClause);
+ }
+
+}
+
+class TestableModelCriteriaWithNamespace extends ModelCriteria
+{
+ public $joins = array();
+
+ public function replaceNames(&$clause)
+ {
+ return parent::replaceNames($clause);
+ }
+}
View
34 test/tools/helpers/namespaces/NamespacesTestBase.php
@@ -0,0 +1,34 @@
+<?php
+
+/**
+ * This file is part of the Propel package.
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @license MIT License
+ */
+
+require_once dirname(__FILE__) . '/../../../../runtime/lib/Propel.php';
+set_include_path(get_include_path() . PATH_SEPARATOR . realpath(dirname(__FILE__) . '/../../../fixtures/namespaced/build/classes'));
+
+/**
+ * Bse class for tests on the schemas schema
+ */
+abstract class NamespacesTestBase extends PHPUnit_Framework_TestCase
+{
+
+ protected function setUp()
+ {
+ parent::setUp();
+ if (!file_exists(dirname(__FILE__) . '/../../../fixtures/namespaced/build/conf/bookstore_namespaced-conf.php')) {
+ $this->markTestSkipped('You must build the namespaced project fot this tests to run');
+ }
+ Propel::init(dirname(__FILE__) . '/../../../fixtures/namespaced/build/conf/bookstore_namespaced-conf.php');
+ }
+
+ protected function tearDown()
+ {
+ parent::tearDown();
+ Propel::init(dirname(__FILE__) . '/../../../fixtures/bookstore/build/conf/bookstore-conf.php');
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.