Browse Source

Fix quoting of field names containing `-`.

Fixes #3165
mark_story 13 years ago
parent
commit
e8b5d81fd7

+ 5 - 3
lib/Cake/Model/Datasource/DboSource.php

@@ -2315,7 +2315,6 @@ class DboSource extends DataSource {
 			}
 			$fields = array_values($fields);
 		}
-
 		if (!$quote) {
 			if (!empty($virtual)) {
 				$fields = array_merge($fields, $this->_constructVirtualFields($model, $alias, $virtual));
@@ -2633,8 +2632,11 @@ class DboSource extends DataSource {
 			$end = preg_quote($this->endQuote);
 		}
 		$conditions = str_replace(array($start, $end), '', $conditions);
-		$conditions = preg_replace_callback('/(?:[\'\"][^\'\"\\\]*(?:\\\.[^\'\"\\\]*)*[\'\"])|([a-z0-9_' . $start . $end . ']*\\.[a-z0-9_' . $start . $end . ']*)/i', array(&$this, '_quoteMatchedField'), $conditions);
-
+		$conditions = preg_replace_callback(
+			'/(?:[\'\"][^\'\"\\\]*(?:\\\.[^\'\"\\\]*)*[\'\"])|([a-z0-9\\-_' . $start . $end . ']*\\.[a-z0-9_\\-' . $start . $end . ']*)/i',
+			array(&$this, '_quoteMatchedField'),
+			$conditions
+		);
 		if ($conditions !== null) {
 			return $conditions;
 		}

+ 5 - 1
lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php

@@ -403,8 +403,12 @@ class TranslateBehaviorTest extends CakeTestCase {
 				)
 			)
 		);
-
 		$this->assertEquals($expected, $result);
+
+		$TestModel = new TranslatedItem();
+		$TestModel->locale = array('pt-br');
+		$result = $TestModel->find('all');
+		$this->assertCount(3, $result, '3 records should have been found, no SQL error.');
 	}
 
 /**

+ 3 - 0
lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php

@@ -2017,6 +2017,9 @@ class MysqlTest extends CakeTestCase {
 		$result = $this->Dbo->conditions('Member.email = "mariano@cricava.com" AND Member.user LIKE "mariano.iglesias%"');
 		$expected = ' WHERE `Member`.`email` = "mariano@cricava.com" AND `Member`.`user` LIKE "mariano.iglesias%"';
 		$this->assertEquals($expected, $result);
+
+		$result = $this->Dbo->conditions('I18n__title_pt-br.content = "test"');
+		$this->assertEquals(' WHERE `I18n__title_pt-br`.`content` = "test"', $result);
 	}
 
 /**

+ 4 - 0
lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php

@@ -585,6 +585,10 @@ class DboSourceTest extends CakeTestCase {
 		$expected = 'Function(`Something`.`foo`) AS `x`';
 		$this->assertEquals($expected, $result);
 
+		$result = $this->testDb->name('I18n__title__pt-br.locale');
+		$expected = '`I18n__title__pt-br`.`locale`';
+		$this->assertEquals($expected, $result);
+
 		$result = $this->testDb->name('name-with-minus');
 		$expected = '`name-with-minus`';
 		$this->assertEquals($expected, $result);