From fd1fa806cf5ce18d0a6dcc4f8fb1bafb4a8005c7 Mon Sep 17 00:00:00 2001 From: Igor Moiseev Date: Mon, 20 Jan 2014 19:42:37 +0100 Subject: 1. Adjustment of code to work with multiple raw_joins 2. Test multiple raw joins --- idiorm.php | 2 +- test/QueryBuilderPsr1Test53.php | 10 ++++++++++ test/QueryBuilderTest.php | 10 ++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/idiorm.php b/idiorm.php index dee8f8b..ed22d8b 100644 --- a/idiorm.php +++ b/idiorm.php @@ -956,7 +956,7 @@ $table .= " {$table_alias}"; } - $this->_values = $parameters; + $this->_values = array_merge($this->_values, $parameters); // Build the constraint if (is_array($constraint)) { diff --git a/test/QueryBuilderPsr1Test53.php b/test/QueryBuilderPsr1Test53.php index 88c5a07..0aa8930 100644 --- a/test/QueryBuilderPsr1Test53.php +++ b/test/QueryBuilderPsr1Test53.php @@ -397,6 +397,16 @@ class QueryBuilderPsr1Test53 extends PHPUnit_Framework_TestCase { $this->assertEquals($expected, ORM::getLastQuery()); } + public function testRawJoinAndRawWhereWithParameters() { + ORM::forTable('widget') + ->rawJoin('INNER JOIN ( SELECT * FROM `widget_handle` WHERE `widget_handle`.name LIKE ? AND `widget_handle`.category = ?)', array('widget_handle.widget_id', '=', 'widget.id'), 'widget_handle', array('%button%', 2)) + ->rawJoin('INNER JOIN ( SELECT * FROM `person` WHERE `person`.name LIKE ?)', array('person.id', '=', 'widget.person_id'), 'person', array('%Fred%')) + ->whereRaw('`id` > ? AND `id` < ?', array(5, 10)) + ->findMany(); + $expected = "SELECT * FROM `widget` INNER JOIN ( SELECT * FROM `widget_handle` WHERE `widget_handle`.name LIKE '%button%' AND `widget_handle`.category = '2') `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id` INNER JOIN ( SELECT * FROM `person` WHERE `person`.name LIKE '%Fred%') `person` ON `person`.`id` = `widget`.`person_id` WHERE `id` > '5' AND `id` < '10'"; + $this->assertEquals($expected, ORM::getLastQuery()); + } + public function testSelectWithDistinct() { ORM::forTable('widget')->distinct()->select('name')->findMany(); $expected = "SELECT DISTINCT `name` FROM `widget`"; diff --git a/test/QueryBuilderTest.php b/test/QueryBuilderTest.php index fc5b45e..3a4a997 100644 --- a/test/QueryBuilderTest.php +++ b/test/QueryBuilderTest.php @@ -397,6 +397,16 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase { $this->assertEquals($expected, ORM::get_last_query()); } + public function testRawJoinAndRawWhereWithParameters() { + ORM::for_table('widget') + ->raw_join('INNER JOIN ( SELECT * FROM `widget_handle` WHERE `widget_handle`.name LIKE ? AND `widget_handle`.category = ?)', array('widget_handle.widget_id', '=', 'widget.id'), 'widget_handle', array('%button%', 2)) + ->raw_join('INNER JOIN ( SELECT * FROM `person` WHERE `person`.name LIKE ?)', array('person.id', '=', 'widget.person_id'), 'person', array('%Fred%')) + ->where_raw('`id` > ? AND `id` < ?', array(5, 10)) + ->find_many(); + $expected = "SELECT * FROM `widget` INNER JOIN ( SELECT * FROM `widget_handle` WHERE `widget_handle`.name LIKE '%button%' AND `widget_handle`.category = '2') `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id` INNER JOIN ( SELECT * FROM `person` WHERE `person`.name LIKE '%Fred%') `person` ON `person`.`id` = `widget`.`person_id` WHERE `id` > '5' AND `id` < '10'"; + $this->assertEquals($expected, ORM::get_last_query()); + } + public function testSelectWithDistinct() { ORM::for_table('widget')->distinct()->select('name')->find_many(); $expected = "SELECT DISTINCT `name` FROM `widget`"; -- cgit v1.2.3