Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added remove support for Document entity.

  • Loading branch information...
commit 0692144a345be5d35bbf51b2a13438b8663cef7b 1 parent 884e839
@engnxe engnxe authored
View
20 data/entity/Document.php
@@ -86,12 +86,20 @@ class Document extends \lithium\data\Entity implements \Iterator, \ArrayAccess {
*/
protected $_valid = false;
+ /**
+ * Removed keys list. Contains names of the fields will be removed from the backend data store
+ *
+ * @var array
+ */
+ protected $_removed = array();
+
protected function _init() {
parent::_init();
$data = (array) $this->_data;
$this->_data = array();
$this->_updated = array();
+ $this->_removed = array();
$this->set($data, array('init' => true));
$this->sync(null, array(), array('materialize' => false));
@@ -145,7 +153,9 @@ public function export(array $options = array()) {
$this->_updated[$key]->_pathKey = "{$path}{$key}";
}
}
- return parent::export($options) + array('key' => $this->_pathKey);
+ return parent::export($options)
+ + array('key' => $this->_pathKey)
+ + array('remove' => $this->_removed);
}
/**
@@ -272,7 +282,13 @@ public function __isset($name) {
* @return void
*/
public function __unset($name) {
- unset($this->_updated[$name]);
+ $parts = explode('.', $name, 2);
+ if (isset($parts[1])) {
+ unset($this->{$parts[0]}[$parts[1]]);
+ } else {
+ unset($this->_updated[$name]);
+ $this->_removed[$name] = true;
+ }
}
/**
View
2  data/source/mongo_db/Exporter.php
@@ -134,7 +134,7 @@ protected static function _diff($left, $right) {
$result = array();
foreach ($left as $key => $value) {
- if (!isset($right[$key]) || $left[$key] !== $right[$key]) {
+ if (!array_key_exists($key, $right) || $left[$key] !== $right[$key]) {
$result[$key] = $value;
}
}
View
56 tests/cases/data/entity/DocumentTest.php
@@ -526,6 +526,61 @@ public function testUnset() {
unset($doc->none);
}
+ public function testUnsetNested() {
+ $data = array(
+ 'a' => 1,
+ 'b' => array(
+ 'ba' => 21,
+ 'bb' => 22
+ ),
+ 'c' => array(
+ 'ca' => 31,
+ 'cb' => array(
+ 'cba' => 321,
+ 'cbb' => 322
+ )
+ ),
+ 'd' => array(
+ 'da' => 41
+ )
+ );
+ $model = $this->_model;
+
+ $doc = new Document(compact('model', 'data'));
+ $expected = $data;
+ $result = $doc->data();
+ $this->assertEqual($expected, $result);
+
+ unset($doc->c->cb->cba);
+ unset($expected['c']['cb']['cba']);
+ $result = $doc->data();
+ $this->assertEqual($expected, $result);
+
+ unset($doc->b->bb);
+ unset($expected['b']['bb']);
+ $result = $doc->data();
+ $this->assertEqual($expected, $result);
+
+ unset($doc->a);
+ unset($expected['a']);
+ $result = $doc->data();
+ $this->assertEqual($expected, $result);
+
+ unset($doc->d);
+ unset($expected['d']);
+ $result = $doc->data();
+ $this->assertEqual($expected, $result);
+
+ $exportedRoot = $doc->export();
+ $this->assertEqual(array('a' => true, 'd' => true), $exportedRoot['remove']);
+
+ $exportedB = $doc->b->export();
+ $this->assertEqual(array('bb' => true), $exportedB['remove']);
+
+ $exportedCCB = $doc->c->cb->export();
+ $this->assertEqual(array('cba' => true), $exportedCCB['remove']);
+ }
+
public function testErrors() {
$doc = new Document(array('data' => array(
'title' => 'Post',
@@ -588,6 +643,7 @@ public function testExport() {
$expected = array(
'data' => array('foo' => 'bar', 'baz' => 'dib'),
'update' => array('foo' => 'bar', 'baz' => 'dib'),
+ 'remove' => array(),
'increment' => array(),
'key' => '',
'exists' => false
Please sign in to comment.
Something went wrong with that request. Please try again.