Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Dec 12, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions data/DocumentSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -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
);
Expand Down
4 changes: 3 additions & 1 deletion data/entity/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 7 additions & 5 deletions data/source/mongo_db/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
29 changes: 29 additions & 0 deletions tests/cases/data/collection/DocumentSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

?>
38 changes: 37 additions & 1 deletion tests/cases/data/source/mongo_db/ExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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]));
}
}

?>