Merge pull request #6605 from cakephp/issue-6602

Fix incorrectly quoted table aliases in virtual fields.
This commit is contained in:
Mark Story 2015-05-22 11:36:44 -04:00
commit d236cffb72
2 changed files with 39 additions and 0 deletions

View file

@ -2869,12 +2869,19 @@ class DboSource extends DataSource {
if (!empty($this->endQuote)) { if (!empty($this->endQuote)) {
$end = preg_quote($this->endQuote); $end = preg_quote($this->endQuote);
} }
// Remove quotes and requote all the Model.field names.
$conditions = str_replace(array($start, $end), '', $conditions); $conditions = str_replace(array($start, $end), '', $conditions);
$conditions = preg_replace_callback( $conditions = preg_replace_callback(
'/(?:[\'\"][^\'\"\\\]*(?:\\\.[^\'\"\\\]*)*[\'\"])|([a-z0-9_][a-z0-9\\-_]*\\.[a-z0-9_][a-z0-9_\\-]*)/i', '/(?:[\'\"][^\'\"\\\]*(?:\\\.[^\'\"\\\]*)*[\'\"])|([a-z0-9_][a-z0-9\\-_]*\\.[a-z0-9_][a-z0-9_\\-]*)/i',
array(&$this, '_quoteMatchedField'), array(&$this, '_quoteMatchedField'),
$conditions $conditions
); );
// Quote `table_name AS Alias`
$conditions = preg_replace(
'/(\s[a-z0-9\\-_.' . $start . $end . ']*' . $end . ')\s+AS\s+([a-z0-9\\-_]+)/i',
'\1 AS ' . $this->startQuote . '\2' . $this->endQuote,
$conditions
);
if ($conditions !== null) { if ($conditions !== null) {
return $conditions; return $conditions;
} }

View file

@ -7836,6 +7836,38 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals(4, $result); $this->assertEquals(4, $result);
} }
/**
* Test virtualfields that contain subqueries get correctly
* quoted allowing reserved words to be used.
*
* @return void
*/
public function testVirtualFieldSubqueryReservedWords() {
$this->loadFixtures('User');
$user = ClassRegistry::init('User');
$user->cacheMethods = false;
$ds = $user->getDataSource();
$sub = $ds->buildStatement(
array(
'fields' => array('Table.user'),
'table' => $ds->fullTableName($user),
'alias' => 'Table',
'limit' => 1,
'conditions' => array(
"Table.id > 1"
)
),
$user
);
$user->virtualFields = array(
'sub_test' => $sub
);
$result = $user->find('first');
$this->assertNotEmpty($result);
}
/** /**
* testVirtualFieldsOrder() * testVirtualFieldsOrder()
* *