Permalink
Browse files

Entities now 'dematerialize' when deleted.

  • Loading branch information...
1 parent 7bc8e25 commit 7fdff6832ff4f5eb9ace1ed8dfa815b1faf1d80e @nateabele nateabele committed Sep 13, 2012
View
@@ -306,17 +306,22 @@ public function exists() {
* @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`.
+ * - `'dematerialize'` _boolean_: If set to `true`, indicates that this entity has
+ * been deleted from the data store and no longer exists. Defaults to `false`.
* @return void
*/
public function sync($id = null, array $data = array(), array $options = array()) {
- $defaults = array('materialize' => true);
+ $defaults = array('materialize' => true, 'dematerialize' => false);
$options += $defaults;
$model = $this->_model;
$key = array();
if ($options['materialize']) {
$this->_exists = true;
}
+ if ($options['dematerialize']) {
+ $this->_exists = false;
+ }
if ($id && $model) {
$key = $model::meta('key');
$key = is_array($key) ? array_combine($key, $id) : array($key => $id);
View
@@ -153,9 +153,10 @@ public function export(array $options = array()) {
$this->_updated[$key]->_pathKey = "{$path}{$key}";
}
}
- return parent::export($options)
- + array('key' => $this->_pathKey)
- + array('remove' => $this->_removed);
+ return parent::export($options) + array(
+ 'key' => $this->_pathKey,
+ 'remove' => $this->_removed
+ );
}
/**
View
@@ -432,14 +432,12 @@ public function update($query, array $options = array()) {
$query = $params['query'];
$params = $query->export($self);
$sql = $self->renderCommand('update', $params, $query);
+ $result = (boolean) $self->invokeMethod('_execute', array($sql));
- if ($self->invokeMethod('_execute', array($sql))) {
- if ($query->entity()) {
- $query->entity()->sync();
- }
- return true;
+ if ($result && is_object($query) && $query->entity()) {
+ $query->entity()->sync();
}
- return false;
+ return $result;
});
}
@@ -456,14 +454,19 @@ public function update($query, array $options = array()) {
public function delete($query, array $options = array()) {
return $this->_filter(__METHOD__, compact('query', 'options'), function($self, $params) {
$query = $params['query'];
+ $isObject = is_object($query);
- if (is_object($query)) {
- $data = $query->export($self);
- $sql = $self->renderCommand('delete', $data, $query);
+ if ($isObject) {
+ $sql = $self->renderCommand('delete', $query->export($self), $query);
} else {
$sql = String::insert($query, $self->value($params['options']));
}
- return (boolean) $self->invokeMethod('_execute', array($sql));
+ $result = (boolean) $self->invokeMethod('_execute', array($sql));
+
+ if ($result && $isObject && $query->entity()) {
+ $query->entity()->sync(null, array(), array('dematerialize' => true));
+ }
+ return $result;
});
}
View
@@ -546,11 +546,17 @@ public function delete($query, array $options = array()) {
$options = $params['options'];
$args = $query->export($self, array('keys' => array('source', 'conditions')));
$source = $args['source'];
+ $conditions = $args['conditions'];
if ($source == "{$_config['gridPrefix']}.files") {
- return $self->invokeMethod('_deleteFile', array($args['conditions']));
+ $result = $self->invokeMethod('_deleteFile', array($conditions));
+ } else {
+ $result = $self->connection->{$args['source']}->remove($conditions, $options);
+ }
+ if ($result && $query->entity()) {
+ $query->entity()->sync(null, array(), array('dematerialize' => true));
}
- return $self->connection->{$args['source']}->remove($args['conditions'], $options);
+ return $result;
});
}
@@ -319,7 +319,12 @@ public function delete($query, array $options = array()) {
$conditions['rev'] = $data['rev'];
}
$result = json_decode($conn->delete("{$config['database']}/{$_path}", $conditions));
- return (isset($result->ok) && $result->ok === true);
+ $result = (isset($result->ok) && $result->ok === true);
+
+ if ($query->entity()) {
+ $query->entity()->sync(null, array(), array('dematerialize' => true));
+ }
+ return $result;
});
}
@@ -523,11 +523,13 @@ public function testDelete() {
'exists' => true
));
$query = new Query(compact('entity') + array('type' => 'delete'));
+ $this->assertTrue($entity->exists());
$this->assertTrue($this->db->delete($query));
$this->assertEqual(1, $query->entity()->id);
$expected = "DELETE FROM {mock_database_posts} WHERE {id} = 1;";
$this->assertEqual($expected, $this->db->sql);
+ $this->assertFalse($entity->exists());
}
public function testOrder() {
@@ -374,6 +374,7 @@ public function testDelete() {
$result = array_pop($this->db->connection->queries);
$conditions = array('_id' => $this->query->entity()->_id);
$this->assertEqual($conditions, $result['conditions']);
+ $this->assertTrue($this->query->entity()->exists());
$model = $this->_model;
$this->query = new Query(compact('model') + array(
@@ -383,6 +384,7 @@ public function testDelete() {
array_push($this->db->connection->results, true);
$this->query->conditions($conditions);
$this->assertTrue($this->db->delete($this->query));
+ $this->assertFalse($this->query->entity()->exists());
$expected = compact('conditions') + array(
'type' => 'remove',

0 comments on commit 7fdff68

Please sign in to comment.