From e5b45bcdea7836b74787a0efeb963c95f8e2c3a5 Mon Sep 17 00:00:00 2001 From: euromark Date: Thu, 30 Oct 2014 19:14:04 +0100 Subject: [PATCH 1/5] Fix IN replacement in virtual fields for MYSQL. --- lib/Cake/Model/Datasource/DboSource.php | 5 ++-- .../Model/Datasource/Database/MysqlTest.php | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/Cake/Model/Datasource/DboSource.php b/lib/Cake/Model/Datasource/DboSource.php index 86516546e..eb3211a1a 100644 --- a/lib/Cake/Model/Datasource/DboSource.php +++ b/lib/Cake/Model/Datasource/DboSource.php @@ -1410,11 +1410,10 @@ class DboSource extends DataSource { protected function _fetchHasMany(Model $Model, $query, $ids) { $ids = array_unique($ids); - $query = str_replace('{$__cakeID__$}', implode(', ', $ids), $query); if (count($ids) > 1) { - $query = str_replace('= (', 'IN (', $query); + $query = str_replace('= ({$__cakeID__$}', 'IN ({$__cakeID__$}', $query); } - + $query = str_replace('{$__cakeID__$}', implode(', ', $ids), $query); return $this->fetchAll($query, $Model->cacheQueries); } diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php index e1a7c2af1..ccd591150 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php @@ -3427,6 +3427,31 @@ SQL; $this->assertEquals($expected, $result); } +/** + * test find() generating usable virtual fields to use in query + * + * @return void + */ + public function testVirtualFieldsWithSubquery() { + $this->loadFixtures('Article', 'Comment', 'User', 'Tag', 'ArticlesTag'); + $this->Dbo->virtualFieldSeparator = '__'; + $Article = ClassRegistry::init('Article'); + $commentsTable = $this->Dbo->fullTableName('comments', false, false); + $Article->Comment->virtualFields = array( + 'extra' => 'SELECT id FROM ' . $commentsTable . ' WHERE id = (SELECT 1)', + ); + $conditions = array('Article.id' => array(1, 2)); + $contain = array('Comment.extra'); + $result = $Article->find('all', compact('conditions', 'contain')); + + $expected = 'SELECT `Comment`.`id`, `Comment`.`article_id`, `Comment`.`user_id`, `Comment`.`comment`, `Comment`.`published`, `Comment`.`created`,' . + ' `Comment`.`updated`, (SELECT id FROM comments WHERE id = (SELECT 1)) AS `Comment__extra`' . + ' FROM `cake_test`.`comments` AS `Comment` WHERE `Comment`.`article_id` IN (1, 2)'; + $test = ConnectionManager::getDatasource('test'); + $log = $test->getLog(); + $this->assertTextEquals($expected, $log['log'][116]['query']); + } + /** * test conditions to generate query conditions for virtual fields * From 70acd2fe3f9e48ff25e26cdf6cdc1d0c9849196f Mon Sep 17 00:00:00 2001 From: euromark Date: Thu, 30 Oct 2014 19:15:23 +0100 Subject: [PATCH 2/5] doc block update. --- lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php index ccd591150..710e9da80 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php @@ -3428,7 +3428,7 @@ SQL; } /** - * test find() generating usable virtual fields to use in query + * test find() generating usable virtual fields to use in query without modifying custom subqueries. * * @return void */ From bb67df4305c87850120ff122476d77591ed92b42 Mon Sep 17 00:00:00 2001 From: euromark Date: Fri, 31 Oct 2014 01:18:08 +0100 Subject: [PATCH 3/5] Simplify test --- lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php index 710e9da80..2bd188aad 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php @@ -3442,14 +3442,17 @@ SQL; ); $conditions = array('Article.id' => array(1, 2)); $contain = array('Comment.extra'); + + $test = ConnectionManager::getDatasource('test'); + $test->getLog(); $result = $Article->find('all', compact('conditions', 'contain')); $expected = 'SELECT `Comment`.`id`, `Comment`.`article_id`, `Comment`.`user_id`, `Comment`.`comment`, `Comment`.`published`, `Comment`.`created`,' . ' `Comment`.`updated`, (SELECT id FROM comments WHERE id = (SELECT 1)) AS `Comment__extra`' . ' FROM `cake_test`.`comments` AS `Comment` WHERE `Comment`.`article_id` IN (1, 2)'; - $test = ConnectionManager::getDatasource('test'); + $log = $test->getLog(); - $this->assertTextEquals($expected, $log['log'][116]['query']); + $this->assertTextEquals($expected, $log['log'][count($log['log']) - 2]['query']); } /** From 0f9890720e07d6b98c751ce0c70fa67be7c9f32b Mon Sep 17 00:00:00 2001 From: euromark Date: Fri, 31 Oct 2014 01:45:41 +0100 Subject: [PATCH 4/5] Correct test --- lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php index 2bd188aad..f8188c4d8 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php @@ -3449,7 +3449,7 @@ SQL; $expected = 'SELECT `Comment`.`id`, `Comment`.`article_id`, `Comment`.`user_id`, `Comment`.`comment`, `Comment`.`published`, `Comment`.`created`,' . ' `Comment`.`updated`, (SELECT id FROM comments WHERE id = (SELECT 1)) AS `Comment__extra`' . - ' FROM `cake_test`.`comments` AS `Comment` WHERE `Comment`.`article_id` IN (1, 2)'; + ' FROM `cakephp_test`.`comments` AS `Comment` WHERE `Comment`.`article_id` IN (1, 2)'; $log = $test->getLog(); $this->assertTextEquals($expected, $log['log'][count($log['log']) - 2]['query']); From 00432fa427230b0005d85e17bf3fa51127248725 Mon Sep 17 00:00:00 2001 From: euromark Date: Sun, 2 Nov 2014 20:44:17 +0100 Subject: [PATCH 5/5] Show stack trace for fatal errors if xdebug is loaded. --- lib/Cake/View/Errors/fatal_error.ctp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/Cake/View/Errors/fatal_error.ctp b/lib/Cake/View/Errors/fatal_error.ctp index f28448d0f..0871555b8 100644 --- a/lib/Cake/View/Errors/fatal_error.ctp +++ b/lib/Cake/View/Errors/fatal_error.ctp @@ -32,3 +32,8 @@ :

+