Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix missing parent assignment for `data\Collection`. Additionnal fix for #577 issue on update. #728

Merged
merged 1 commit into from

2 participants

@jails
Collaborator

No description provided.

@nateabele nateabele merged commit b62d6c7 into UnionOfRAD:dev

1 check failed

Details default The Travis build failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 12, 2012
  1. @jails

    Fix missing parent assignment for `data\Collection`. Additionnal fix …

    jails authored
    …for #577 issue on update.
This page is out of date. Refresh to see the latest.
View
10 data/DocumentSchema.php
@@ -23,7 +23,13 @@ protected function _init() {
}
public function cast($object, $key, $data, array $options = array()) {
- $defaults = array('pathKey' => null, 'model' => null, 'wrap' => true, 'first' => false);
+ $defaults = array(
+ 'parent' => null,
+ 'pathKey' => null,
+ 'model' => null,
+ 'wrap' => true,
+ 'first' => false
+ );
$options += $defaults;
$basePathKey = $options['pathKey'];
@@ -69,7 +75,7 @@ protected function _castArray($object, $val, $pathKey, $options, $defaults) {
if ($options['wrap']) {
$config = array(
'data' => $val,
- 'parent' => $object,
+ 'parent' => $options['parent'],
'model' => $options['model'],
'schema' => $this
);
View
4 data/entity/Document.php
@@ -137,6 +137,7 @@ public function &__get($name) {
'class' => 'set',
'schema' => $this->schema(),
'pathKey' => $this->_pathKey ? $this->_pathKey . '.' . $name : $name,
+ 'parent' => $this,
'model' => $this->_model
));
return $this->_updated[$name];
@@ -319,7 +320,8 @@ public function set(array $data, array $options = array()) {
if ($cast) {
$pathKey = $this->_pathKey;
$model = $this->_model;
- $val = $schema->cast($this, $key, $val, compact('pathKey', 'model'));
+ $parent = $this;
+ $val = $schema->cast($this, $key, $val, compact('pathKey', 'model', 'parent'));
}
if ($val instanceof self) {
$val->_exists = $options['init'] && $this->_exists;
View
12 data/source/mongo_db/Exporter.php
@@ -110,15 +110,17 @@ protected static function _update($export) {
$original = $export['data'];
$isArray = is_object($value) && get_class($value) == static::$_classes['set'];
- $options = array('handlers' => array(
- 'MongoDate' => function($value) { return $value; },
- 'MongoId' => function($value) { return $value; }
- ));
+ $options = array(
+ 'indexed' => null,
+ 'handlers' => array(
+ 'MongoDate' => function($value) { return $value; },
+ 'MongoId' => function($value) { return $value; }
+ )
+ );
if ($isArray) {
$newValue = $value->to('array', $options);
$originalValue = null;
-
if (isset($original[$key])) {
$originalValue = $original[$key]->to('array', $options);
}
View
29 tests/cases/data/collection/DocumentSetTest.php
@@ -300,6 +300,35 @@ public function testTo() {
);
$this->assertEqual($expected, $doc->to('array', array('indexed' => false)));
}
+
+ public function testParent() {
+ $model = $this->_model;
+ $schema = new Schema(array('fields' => array(
+ '_id' => array('type' => 'id'),
+ 'bar' => array('array' => true),
+ 'foo' => array('type' => 'object', 'array' => true),
+ 'foo.foo' => array('type' => 'integer'),
+ 'foo.bar' => array('type' => 'integer')
+ )));
+ $doc = new Document(compact('model', 'schema'));
+
+ $expected = array(
+ 'foo' => 1,
+ 'bar' => 2
+ );
+ $doc->foo[] = $expected;
+ $this->assertEqual($doc, $doc->foo->parent());
+ $this->assertEqual($expected, $doc->foo[0]->data());
+
+ $data = array(
+ '_id' => '4fb6e2df3e91581fe6e75737',
+ 'foo' => array($expected)
+ );
+
+ $doc = new Document(compact('model', 'schema', 'data'));
+ $this->assertEqual($doc, $doc->foo->parent());
+ $this->assertEqual($expected, $doc->foo[0]->data());
+ }
}
?>
View
38 tests/cases/data/source/mongo_db/ExporterTest.php
@@ -688,7 +688,7 @@ public function testToData() {
$this->assertEqual('Bar2', $accounts['4fb6e2df3e91581fe6e75739']['name']);
}
- public function testIndexesOnExport() {
+ public function testIndexesOnExportingDocumentSet() {
$schema = new Schema(array('fields' => array(
'_id' => array('type' => 'id'),
'accounts' => array('type' => 'object', 'array' => true),
@@ -743,6 +743,42 @@ public function testIndexesOnExport() {
$this->assertTrue(isset($result['update'][1]['accounts'][0]));
$this->assertTrue(isset($result['update'][1]['accounts'][1]));
}
+
+ public function testIndexesOnExportingDocument() {
+ $schema = new Schema(array('fields' => array(
+ '_id' => array('type' => 'id'),
+ 'accounts' => array('type' => 'object', 'array' => true),
+ 'accounts._id' => array('type' => 'id'),
+ 'accounts.name' => array('type' => 'string')
+ )));
+
+ $data = array(
+ '_id' => '4c8f86167675abfabd970300',
+ 'accounts' => array(array(
+ '_id' => "4fb6e2dd3e91581fe6e75736",
+ 'name' => 'Foo1'
+ ),array(
+ '_id' => "4fb6e2df3e91581fe6e75737",
+ 'name' => 'Bar1'
+ ))
+ );
+
+ $model = $this->_model;
+
+ $document = new Document(compact('model', 'schema', 'data'));
+ $this->assertTrue($document->accounts instanceof DocumentSet);
+ $this->assertTrue($document->accounts instanceof DocumentSet);
+
+ $export = $document->export();
+ $result = Exporter::get('create', $document->export());
+ $this->assertTrue(isset($result['create']['accounts'][0]));
+ $this->assertTrue(isset($result['create']['accounts'][1]));
+
+ $export['data'] = array();
+ $result = Exporter::get('update', $export);
+ $this->assertTrue(isset($result['update']['accounts'][0]));
+ $this->assertTrue(isset($result['update']['accounts'][1]));
+ }
}
?>
Something went wrong with that request. Please try again.