From f89b1aa181811ae4bb8478530b8c20ecdb9a5b41 Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=A9sar=20D=2E=20Rodas?= Date: Tue, 1 Jun 2010 15:30:19 -0400 Subject: [PATCH] - Improved addIndex - Accepts non-indexes names - Accepts list of columns array('col1', 'col2'); - Accepts normal way array('col1' => 1, 'col2' => -1); --- lib/ActiveMongo.php | 11 +++++++++++ tests/Models.php | 8 +++++++- tests/QueryTest.php | 12 ++++++++++-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/ActiveMongo.php b/lib/ActiveMongo.php index c3c0f30..ce83ba3 100644 --- a/lib/ActiveMongo.php +++ b/lib/ActiveMongo.php @@ -967,6 +967,17 @@ abstract class ActiveMongo implements Iterator, Countable, ArrayAccess 'background' => 1, ); + if (!is_array($columns)) { + $columns = array($columns => 1); + } + + foreach ($columns as $id => $name) { + if (is_numeric($id)) { + unset($columns[$id]); + $columns[$name] = 1; + } + } + foreach ($default_options as $option => $value) { if (!isset($options[$option])) { $options[$option] = $value; diff --git a/tests/Models.php b/tests/Models.php index 30c9eee..56b3958 100644 --- a/tests/Models.php +++ b/tests/Models.php @@ -19,7 +19,8 @@ class Model1 extends ActiveMongo function setup() { - $this->addIndex(array('a' => 1)); + $this->addIndex('b'); + $this->addIndex(array('a' => -1)); } } @@ -44,6 +45,11 @@ class Model2 extends ActiveMongo } } + function setup() + { + $this->addIndex(array('M1')); + } + function update_refs($m1) { /* reset just in case */ diff --git a/tests/QueryTest.php b/tests/QueryTest.php index db08818..fd23db6 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -48,8 +48,16 @@ class QueryTest extends PHPUnit_Framework_TestCase function testInstall() { ActiveMongo::install(); - $indexes = Model1::getIndexes(); - $this->assertTrue(isset($indexes[1]['key']['a'])); + $index = Model1::getIndexes(); + $this->assertTrue(isset($index[1]['key']['b'])); + $this->assertTrue(isset($index[2]['key']['a'])); + $this->assertEquals($index[1]['key']['b'], 1); + $this->assertEquals($index[2]['key']['a'], -1); + $this->asserTEquals(count($index), 3); + $index = Model2::getIndexes(); + $this->assertTrue(isset($index[1]['key']['M1'])); + $this->assertEquals($index[1]['key']['M1'], 1); + $this->asserTEquals(count($index), 2); } /** -- 2.11.4.GIT