summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Holywell <[email protected]>2014-05-28 15:25:18 +0100
committerSimon Holywell <[email protected]>2014-05-28 15:25:18 +0100
commit9b3b9caa2232c097cbfff153e9c8f99d72248049 (patch)
treed29bdcd9deaec5ee9aa0d1cc07b2cd17359ff267
parentde4a611d5c3676e3b505c4a6812ccc91ec049d61 (diff)
parentdb071216909799fcc781f9257065f7411214c169 (diff)
Merge pull request #202 from lrlopez/where_id_in-2
where_id_in() for selecting multiple records by primary key
-rw-r--r--docs/querying.rst6
-rw-r--r--idiorm.php24
-rw-r--r--test/QueryBuilderTest.php15
3 files changed, 45 insertions, 0 deletions
diff --git a/docs/querying.rst b/docs/querying.rst
index f86f162..bf140a1 100644
--- a/docs/querying.rst
+++ b/docs/querying.rst
@@ -275,6 +275,12 @@ Respects the ID column specified in the config. If you are using a compound
primary key, you must pass an array where the key is the column name. Columns
that don't belong to the key will be ignored.
+Shortcut: ``where_id_in``
+'''''''''''''''''''''''''
+
+This helper method is similar to ``where_id_is`, but it expects an array of
+primary keys to be selected. It is compound primary keys aware.
+
Less than / greater than: ``where_lt``, ``where_gt``, ``where_lte``, ``where_gte``
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
diff --git a/idiorm.php b/idiorm.php
index ff0ff6e..73072e6 100644
--- a/idiorm.php
+++ b/idiorm.php
@@ -1173,6 +1173,18 @@
return $filtered;
}
+ /**
+ * Helper method that filters an array containing compound column/value
+ * arrays.
+ */
+ protected function _get_compound_id_column_values_array($values) {
+ $filtered = array();
+ foreach($values as $value) {
+ $filtered[] = $this->_get_compound_id_column_values($value);
+ }
+ return $filtered;
+ }
+
/**
* Add a WHERE column = value clause to your query. Each time
* this is called in the chain, an additional WHERE will be
@@ -1251,6 +1263,18 @@
}
/**
+ * Similar to where_id_is() but allowing multiple primary keys.
+ *
+ * If primary key is compound, only the columns that
+ * belong to they key will be used for the query
+ */
+ public function where_id_in($ids) {
+ return (is_array($this->_get_id_column_name())) ?
+ $this->where_any_is($this->_get_compound_id_column_values_array($ids)) :
+ $this->where_in($this->_get_id_column_name(), $ids);
+ }
+
+ /**
* Add a WHERE ... LIKE clause to your query.
*/
public function where_like($column_name, $value=null) {
diff --git a/test/QueryBuilderTest.php b/test/QueryBuilderTest.php
index 24630d9..9c4ef97 100644
--- a/test/QueryBuilderTest.php
+++ b/test/QueryBuilderTest.php
@@ -40,6 +40,12 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($expected, ORM::get_last_query());
}
+ public function testWhereIdIn() {
+ ORM::for_table('widget')->where_id_in(array(4, 5))->find_many();
+ $expected = "SELECT * FROM `widget` WHERE `id` IN ('4', '5')";
+ $this->assertEquals($expected, ORM::get_last_query());
+ }
+
public function testSingleWhereClause() {
ORM::for_table('widget')->where('name', 'Fred')->find_one();
$expected = "SELECT * FROM `widget` WHERE `name` = 'Fred' LIMIT 1";
@@ -617,6 +623,15 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($expected, ORM::get_last_query());
}
+ public function testWhereIdInWithCompoundPrimaryKey() {
+ $record = ORM::for_table('widget')->use_id_column(array('id1', 'id2'));
+ $record->where_id_in(array(
+ array('id1' => 10, 'name' => 'Joe', 'id2' => 20),
+ array('id1' => 20, 'name' => 'Joe', 'id2' => 30)))->find_many();
+ $expected = "SELECT * FROM `widget` WHERE (( `id1` = '10' AND `id2` = '20' ) OR ( `id1` = '20' AND `id2` = '30' ))";
+ $this->assertEquals($expected, ORM::get_last_query());
+ }
+
/**
* Regression tests
*/