Skip to content

Commit

Permalink
Fix quoting of field names containing -.
Browse files Browse the repository at this point in the history
Fixes #3165
  • Loading branch information
markstory committed Aug 31, 2012
1 parent 3e17c7a commit e8b5d81
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
8 changes: 5 additions & 3 deletions lib/Cake/Model/Datasource/DboSource.php
Expand Up @@ -2315,7 +2315,6 @@ public function fields(Model $model, $alias = null, $fields = array(), $quote =
}
$fields = array_values($fields);
}

if (!$quote) {
if (!empty($virtual)) {
$fields = array_merge($fields, $this->_constructVirtualFields($model, $alias, $virtual));
Expand Down Expand Up @@ -2633,8 +2632,11 @@ protected function _quoteFields($conditions) {
$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;
}
Expand Down
6 changes: 5 additions & 1 deletion lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php
Expand Up @@ -403,8 +403,12 @@ public function testLocaleMultiple() {
)
)
);

$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.');
}

/**
Expand Down
3 changes: 3 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php
Expand Up @@ -2017,6 +2017,9 @@ public function testQuotesInStringConditions() {
$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);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
Expand Up @@ -585,6 +585,10 @@ public function testName() {
$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);
Expand Down

0 comments on commit e8b5d81

Please sign in to comment.