Skip to content

Commit

Permalink
Renaming Entity::update() to Entity::sync() to better reflect fun…
Browse files Browse the repository at this point in the history
…ctionality, and reduce API ambiguity.
  • Loading branch information
nateabele committed Aug 15, 2011
1 parent d6a8d0e commit b58092f
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 21 deletions.
11 changes: 9 additions & 2 deletions data/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,20 @@ public function exists() {
* @see lithium\data\Model::save()
* @param mixed $id The ID to assign, where applicable.
* @param array $data Any additional generated data assigned to the object by the database.
* @param array $options Method options:
* - `'materialize'` _boolean_: Determines whether or not the flag should be set
* that indicates that this entity exists in the data store. Defaults to `true`.
* @return void
*/
public function update($id = null, array $data = array()) {
$this->_exists = true;
public function sync($id = null, array $data = array(), array $options = array()) {
$defaults = array('materialize' => true);
$options += $defaults;
$model = $this->_model;
$key = array();

if ($options['materialize']) {
$this->_exists = true;
}
if ($id && $model) {
$key = $model::meta('key');
$key = is_array($key) ? array_combine($key, $id) : array($key => $id);
Expand Down
2 changes: 1 addition & 1 deletion data/collection/DocumentArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function exists() {
return $this->_exists;
}

public function update($id = null, array $data = array()) {
public function sync($id = null, array $data = array()) {
$this->_exists = true;
$this->_data = $data ?: $this->_data;
}
Expand Down
28 changes: 22 additions & 6 deletions data/entity/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,31 @@ public function export() {
return parent::export() + array('key' => $this->_pathKey, 'remove' => $this->_removed);
}

public function update($id = null, array $data = array()) {
parent::update($id, $data);
/**
* Extends the parent implementation to ensure that child documents are properly synced as well.
*
* @param mixed $id
* @param array $data
* @param 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
*/
public function sync($id = null, array $data = array(), array $options = array()) {
$defaults = array('recursive' => true);
$options += $defaults;

if (!$options['recursive']) {
return parent::sync($id, $data, $options);
}

foreach ($this->_data as $key => $val) {
if (is_object($val) && method_exists($val, 'update')) {
$this->_data[$key]->update(null, isset($data[$key]) ? $data[$key] : array());
foreach ($this->_updated as $key => $val) {
if (is_object($val) && method_exists($val, 'sync')) {
$nested = isset($data[$key]) ? $data[$key] : array();
$this->_updated[$key]->sync(null, $nested, $options);
}
}
$this->_removed = array();
parent::sync($id, $data, $options);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions data/source/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public function create($query, array $options = array()) {
if (($model) && !$model::key($entity)) {
$id = $self->invokeMethod('_insertId', array($object));
}
$entity->update($id);
$entity->sync($id);
}
return true;
});
Expand Down Expand Up @@ -335,7 +335,7 @@ public function update($query, array $options = array()) {

if ($self->invokeMethod('_execute', array($sql))) {
if ($query->entity()) {
$query->entity()->update();
$query->entity()->sync();
}
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions data/source/MongoDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ public function create($query, array $options = array()) {

if ($result === true || isset($result['ok']) && (boolean) $result['ok'] === true) {
if ($query->entity()) {
$query->entity()->update($data['create']['_id']);
$query->entity()->sync($data['create']['_id']);
}
return true;
}
Expand Down Expand Up @@ -528,7 +528,7 @@ public function update($query, array $options = array()) {
$update = array('$set' => $update);
}
if ($self->connection->{$source}->update($args['conditions'], $update, $options)) {
$query->entity() ? $query->entity()->update() : null;
$query->entity() ? $query->entity()->sync() : null;
return true;
}
return false;
Expand Down
4 changes: 2 additions & 2 deletions data/source/http/adapter/CouchDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public function create($query, array $options = array()) {

if (isset($result['_id']) || (isset($result['ok']) && $result['ok'] === true)) {
$result = $self->invokeMethod('_format', array($result, $options));
$query->entity()->update($result['id'], $result);
$query->entity()->sync($result['id'], $result);
return true;
}
return false;
Expand Down Expand Up @@ -281,7 +281,7 @@ public function update($query, array $options = array()) {

if (isset($result['_id']) || (isset($result['ok']) && $result['ok'] === true)) {
$result = $self->invokeMethod('_format', array($result, $options));
$query->entity()->update($result['id'], array('rev' => $result['rev']));
$query->entity()->sync($result['id'], array('rev' => $result['rev']));
return true;
}
if (isset($result['error'])) {
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/data/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testIncrement() {

$this->assertFalse(isset($entity->bar));
$entity->bar = 'blah';
$entity->update();
$entity->sync();

$this->expectException("/^Field 'bar' cannot be incremented.$/");
$entity->increment('bar');
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/data/entity/RecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function testSetData() {

public function testRecordExists() {
$this->assertFalse($this->record->exists());
$this->record->update(313);
$this->record->sync(313);
$this->assertIdentical(313, $this->record->id);
$this->assertTrue($this->record->exists());

Expand Down
7 changes: 3 additions & 4 deletions tests/cases/data/source/mongo_db/ExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,6 @@ public function testFieldRemoval() {
/**
* Tests that when an existing object is attached as a value of another existing object, the
* whole sub-object is re-written to the new value.
*
* @return void
*/
public function testAppendExistingObjects() {
$doc = new Document(array('exists' => true, 'data' => array(
Expand All @@ -188,15 +186,15 @@ public function testAppendExistingObjects() {
$result = Exporter::get('update', $doc->export());
$expected = array('update' => array('deeply' => array('foo' => 'bar')));
$this->assertEqual($expected, $result);
$doc->update();

$expected = array('$set' => array('deeply' => array('foo' => 'bar')));
$this->assertEqual($expected, Exporter::toCommand($result));

$doc->sync();
$doc->append2 = new Document(array('exists' => false, 'data' => array('foo' => 'bar')));
$expected = array('update' => array('append2' => array('foo' => 'bar')));
$this->assertEqual($expected, Exporter::get('update', $doc->export()));
$doc->update();
$doc->sync();

$this->assertFalse(Exporter::get('update', $doc->export()));
$doc->append2->foo = 'baz';
Expand All @@ -206,6 +204,7 @@ public function testAppendExistingObjects() {
$expected = array('update' => array(
'append2.foo' => 'baz', 'append2.bar' => 'dib', 'deeply.nested' => true
));

$this->assertEqual($expected, Exporter::get('update', $doc->export()));
}

Expand Down

0 comments on commit b58092f

Please sign in to comment.