summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--idiorm.php4
-rw-r--r--test/QueryBuilderPsr1Test53.php12
-rw-r--r--test/QueryBuilderTest.php12
3 files changed, 27 insertions, 1 deletions
diff --git a/idiorm.php b/idiorm.php
index 4ab91bc..dee8f8b 100644
--- a/idiorm.php
+++ b/idiorm.php
@@ -949,13 +949,15 @@
/**
* Add a RAW JOIN source to the query
*/
- public function raw_join($table, $constraint, $table_alias=null) {
+ public function raw_join($table, $constraint, $table_alias, $parameters = array()) {
// Add table alias if present
if (!is_null($table_alias)) {
$table_alias = $this->_quote_identifier($table_alias);
$table .= " {$table_alias}";
}
+ $this->_values = $parameters;
+
// Build the constraint
if (is_array($constraint)) {
list($first_column, $operator, $second_column) = $constraint;
diff --git a/test/QueryBuilderPsr1Test53.php b/test/QueryBuilderPsr1Test53.php
index a51103e..88c5a07 100644
--- a/test/QueryBuilderPsr1Test53.php
+++ b/test/QueryBuilderPsr1Test53.php
@@ -385,6 +385,18 @@ class QueryBuilderPsr1Test53 extends PHPUnit_Framework_TestCase {
$this->assertEquals($expected, ORM::getLastQuery());
}
+ public function testRawJoin() {
+ ORM::forTable('widget')->rawJoin('INNER JOIN ( SELECT * FROM `widget_handle` )', array('widget_handle.widget_id', '=', 'widget.id'), 'widget_handle')->findMany();
+ $expected = "SELECT * FROM `widget` INNER JOIN ( SELECT * FROM `widget_handle` ) `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`";
+ $this->assertEquals($expected, ORM::getLastQuery());
+ }
+
+ public function testRawJoinWithParameters() {
+ 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))->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`";
+ $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 ebd2833..fc5b45e 100644
--- a/test/QueryBuilderTest.php
+++ b/test/QueryBuilderTest.php
@@ -385,6 +385,18 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($expected, ORM::get_last_query());
}
+ public function testRawJoin() {
+ ORM::for_table('widget')->raw_join('INNER JOIN ( SELECT * FROM `widget_handle` )', array('widget_handle.widget_id', '=', 'widget.id'), 'widget_handle')->find_many();
+ $expected = "SELECT * FROM `widget` INNER JOIN ( SELECT * FROM `widget_handle` ) `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`";
+ $this->assertEquals($expected, ORM::get_last_query());
+ }
+
+ public function testRawJoinWithParameters() {
+ 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))->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`";
+ $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`";