/** * Saves model data to the database. * * @return array */ public function save() { $this->saveRelationships(); $this->appendRelationshipData(); $type = $this->exists() ? 'update' : 'create'; $doc = new Document(); $doc->set($this->to_a()); $query = new Query(array('entity' => $doc, 'model' => get_class($this), 'conditions' => array($this->primaryKey => $this->{$this->primaryKey}))); $db = static::connection(); $result = $db->{$type}($query); $exported = $doc->export(); $this->exists(true); $this->data[$this->primaryKey] = $exported['update'][$this->primaryKey]; return $result; }
/** * Tests that a modified `Document` exports the proper fields in a newly-appended nested * `Document`. * * @return void */ public function testModifiedExport() { $database = new MockDocumentSource(); $model = $this->_model; $data = array('foo' => 'bar', 'baz' => 'dib'); $doc = new Document(compact('model', 'data')); $doc->nested = array('more' => 'data'); $newData = $doc->export($database); $expected = array('foo' => 'bar', 'baz' => 'dib', 'nested' => array('more' => 'data')); $this->assertEqual($expected, $newData); $doc = new Document(array('model' => $model, 'exists' => true, 'data' => array('foo' => 'bar', 'baz' => 'dib'))); $this->assertFalse($doc->export($database)); $doc->nested = array('more' => 'data'); $this->assertEqual('data', $doc->nested->more); $modified = $doc->export($database); $this->assertEqual(array('nested' => array('more' => 'data')), $modified); $doc->update(); $this->assertFalse($doc->export($database)); $doc->more = 'cowbell'; $doc->nested->evenMore = 'cowbell'; $modified = $doc->export($database); $expected = array('nested' => array('evenMore' => 'cowbell'), 'more' => 'cowbell'); $this->assertEqual($expected, $modified); $doc->update(); $doc->nested->evenMore = 'foo!'; $this->assertEqual(array('nested' => array('evenMore' => 'foo!')), $doc->export($database)); }
/** * 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']); }
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])); }
/** * 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); }
public function testWithArraySchema() { $model = $this->_model; $model::schema(array('_id' => array('type' => 'id'), 'list' => array('array' => true), 'list.foo' => array('type' => 'string'), 'list.bar' => array('type' => 'string'))); $doc = new Document(compact('model')); $doc->list[] = array('foo' => '!!', 'bar' => '??'); $data = array('list' => array(array('foo' => '!!', 'bar' => '??'))); $this->assertEqual($data, $doc->data()); $result = Exporter::get('create', $doc->export()); $this->assertEqual($data, $result['create']); $result = Exporter::get('update', $doc->export()); $this->assertEqual($data, $result['update']); $doc = new Document(compact('model')); $doc->list = array(); $doc->list[] = array('foo' => '!!', 'bar' => '??'); $data = array('list' => array(array('foo' => '!!', 'bar' => '??'))); $this->assertEqual($data, $doc->data()); $result = Exporter::get('create', $doc->export()); $this->assertEqual($result['create'], $data); $result = Exporter::get('update', $doc->export()); $this->assertEqual($result['update'], $data); }