Extends the parent implementation to ensure that child documents are properly synced as well.
public sync ( mixed $id = null, array $data = [], array $options = [] ) : void | ||
$id | mixed | |
$data | array | |
$options | array | Options when calling this method: - `'recursive'` _boolean_: If `true` attempts to sync nested objects as well. Otherwise, only syncs the current object. Defaults to `true`. |
return | void |
/** * Tests that arrays of nested objects can be appended to and will be updated using the proper * MongoDB operators. */ public function testAppendingNestedObjectArray() { $model = $this->_model; $model::schema(false); $model::schema(array('accounts' => array('type' => 'object', 'array' => true), 'accounts.name' => array('type' => 'string'))); $doc = new Document(compact('model')); $this->assertEqual(array(), $doc->accounts->data()); $doc->sync(); $data = array('name' => 'New account'); $doc->accounts[] = new Document(compact('data')); $result = Exporter::get('update', $doc->export()); $expected = array('update' => array('accounts.0' => $data)); $this->assertEqual($expected, $result); $result = Exporter::toCommand($result); $expected = array('$set' => array('accounts.0' => $data)); $this->assertEqual($expected, $result); }
/** * Tests that a modified `Document` exports the proper fields in a newly-appended nested * `Document`. */ public function testModifiedExport() { $model = $this->_model; $data = array('foo' => 'bar', 'baz' => 'dib'); $doc = new Document(compact('model', 'data') + array('exists' => false)); $doc->nested = array('more' => 'data'); $newData = $doc->export(); $expected = array('foo' => 'bar', 'baz' => 'dib', 'nested.more' => 'data'); $this->assertFalse($newData['exists']); $this->assertEqual(array('foo' => 'bar', 'baz' => 'dib'), $newData['data']); $this->assertCount(3, $newData['update']); $this->assertInstanceOf('lithium\\data\\entity\\Document', $newData['update']['nested']); $result = $newData['update']['nested']->export(); $this->assertFalse($result['exists']); $this->assertEqual(array('more' => 'data'), $result['data']); $this->assertEqual(array('more' => 'data'), $result['update']); $this->assertEqual('nested', $result['key']); $doc = new Document(compact('model') + array('exists' => true, 'data' => array('foo' => 'bar', 'baz' => 'dib'))); $result = $doc->export(); $this->assertEqual($result['data'], $result['update']); $doc->nested = array('more' => 'data'); $this->assertEqual('data', $doc->nested->more); $modified = $doc->export(); $this->assertTrue($modified['exists']); $this->assertEqual(array('foo' => 'bar', 'baz' => 'dib'), $modified['data']); $this->assertEqual(array('foo', 'baz', 'nested'), array_keys($modified['update'])); $this->assertNull($modified['key']); $nested = $modified['update']['nested']->export(); $this->assertFalse($nested['exists']); $this->assertEqual(array('more' => 'data'), $nested['data']); $this->assertEqual('nested', $nested['key']); $doc->sync(); $result = $doc->export(); $this->assertEqual($result['data'], $result['update']); $doc->more = 'cowbell'; $doc->nested->evenMore = 'cowbell'; $modified = $doc->export(); $expected = array('more' => 'cowbell') + $modified['data']; $this->assertEqual($expected, $modified['update']); $this->assertEqual(array('foo', 'baz', 'nested'), array_keys($modified['data'])); $this->assertEqual('bar', $modified['data']['foo']); $this->assertEqual('dib', $modified['data']['baz']); $this->assertTrue($modified['exists']); $nested = $modified['data']['nested']->export(); $this->assertTrue($nested['exists']); $this->assertEqual(array('more' => 'data'), $nested['data']); $this->assertEqual(array('evenMore' => 'cowbell') + $nested['data'], $nested['update']); $this->assertEqual('nested', $nested['key']); $doc->sync(); $doc->nested->evenMore = 'foo!'; $modified = $doc->export(); $this->assertEqual($modified['data'], $modified['update']); $nested = $modified['data']['nested']->export(); $this->assertEqual(array('evenMore' => 'foo!') + $nested['data'], $nested['update']); }
/** * Tests that a nested key on a previously saved document gets updated properly. */ public function testExistingNestedKeyOverwrite() { $doc = new Document(array('model' => $this->_model)); $doc->{'this.that'} = 'value1'; $this->assertEqual(array('this' => array('that' => 'value1')), $doc->data()); $result = Exporter::get('create', $doc->export()); $this->assertEqual(array('create' => array('this' => array('that' => 'value1'))), $result); $doc->sync(); $doc->{'this.that'} = 'value2'; $this->assertEqual(array('this' => array('that' => 'value2')), $doc->data()); $result = Exporter::get('update', $doc->export()); $this->assertEqual(array('update' => array('this.that' => 'value2')), $result); }