Browse Source

Mongo "_id" processing advanced.

tags/2.0.0-beta
Klimov Paul 11 years ago
parent
commit
27a1c63e26
  1. 25
      extensions/mongo/ActiveRecord.php
  2. 10
      extensions/mongo/Collection.php
  3. 4
      tests/unit/extensions/mongo/ActiveRelationTest.php
  4. 12
      tests/unit/extensions/mongo/QueryRunTest.php

25
extensions/mongo/ActiveRecord.php

@ -1157,4 +1157,29 @@ abstract class ActiveRecord extends Model
throw new InvalidParamException(get_class($this) . ' has no relation named "' . $name . '".', 0, $e);
}
}
/**
* Sets the element at the specified offset.
* This method is required by the SPL interface `ArrayAccess`.
* It is implicitly called when you use something like `$model[$offset] = $item;`.
* @param integer $offset the offset to set element
* @param mixed $item the element value
* @throws \Exception on failure
*/
public function offsetSet($offset, $item)
{
// Bypass relation owner restriction to 'yii\db\ActiveRecord' at [[yii\db\ActiveRelationTrait::findWith()]]:
try {
$relation = $this->getRelation($offset);
if (is_object($relation)) {
$this->populateRelation($offset, $item);
return;
}
} catch (InvalidParamException $e) {
// shut down exception : has getter, but not relation
} catch (UnknownMethodException $e) {
throw $e->getPrevious();
}
parent::offsetSet($offset, $item);
}
}

10
extensions/mongo/Collection.php

@ -267,8 +267,18 @@ class Collection extends Object
$key = $this->normalizeConditionKeyword($key);
if (strncmp('$', $key, 1) !== 0 && is_array($actualValue) && array_key_exists(0, $actualValue)) {
// shortcut for IN condition
if ($key == '_id') {
foreach ($actualValue as &$actualValuePart) {
if (!is_object($actualValuePart)) {
$actualValuePart = new \MongoId($actualValuePart);
}
}
}
$result[$key]['$in'] = $actualValue;
} else {
if ($key == '_id' && !is_object($actualValue)) {
$actualValue = new \MongoId($actualValue);
}
$result[$key] = $actualValue;
}
}

4
tests/unit/extensions/mongo/ActiveRelationTest.php

@ -77,7 +77,7 @@ class ActiveRelationTest extends MongoTestCase
$this->assertEquals(10, count($orders));
$this->assertTrue($orders[0]->isRelationPopulated('customer'));
$this->assertTrue($orders[1]->isRelationPopulated('customer'));
$this->assertTrue($orders[0]->index instanceof ArticleIndex);
$this->assertTrue($orders[1]->index instanceof ArticleIndex);
$this->assertTrue($orders[0]->customer instanceof Customer);
$this->assertTrue($orders[1]->customer instanceof Customer);
}
}

12
tests/unit/extensions/mongo/QueryRunTest.php

@ -105,4 +105,16 @@ class QueryRunTest extends MongoTestCase
->all($connection);
$this->assertEquals('name9', $rows[0]['name']);
}
public function testMatchPlainId()
{
$connection = $this->getConnection();
$query = new Query;
$row = $query->from('customer')->one($connection);
$query = new Query;
$rows = $query->from('customer')
->where(['_id' => $row['_id']->__toString()])
->all($connection);
$this->assertEquals(1, count($rows));
}
}
Loading…
Cancel
Save