Skip to content

Commit

Permalink
Merge pull request #296 from montymxb/bidir-fix
Browse files Browse the repository at this point in the history
Fixes save issue with bi-directional relations via pointers
  • Loading branch information
Benjamin Wilson Friedman committed Mar 24, 2017
2 parents cb40054 + ba1bc60 commit 66811f5
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Parse/ParseObject.php
Expand Up @@ -279,7 +279,7 @@ private function hasDirtyChildren()
$this->estimatedData,
function ($object) use (&$result) {
if ($object instanceof ParseObject) {
if ($object->isDirty()) {
if ($object->_isDirty(false)) {
$result = true;
}
}
Expand Down
44 changes: 44 additions & 0 deletions tests/Parse/ParseObjectTest.php
Expand Up @@ -1149,6 +1149,9 @@ public function testGetAllKeys()

}

/**
* @group dirty-children
*/
public function testDirtyChildren()
{
$obj = new ParseObject('TestClass');
Expand All @@ -1161,10 +1164,51 @@ public function testDirtyChildren()
$this->assertFalse($obj->isDirty());

$obj->set('innerObject', $obj2);
$this->assertTrue($obj->isDirty());

$this->assertTrue($obj2->isDirty());

$obj->save();
$this->assertFalse($obj->isDirty());
$this->assertFalse($obj2->isDirty());


// update the child again
$obj2->set('key2', 'an unsaved value');
$this->assertTrue($obj->isDirty());
$obj->save();


// test setting a child in child
$obj3 = new ParseObject('TestClass');
$obj3->set('key2', 'child of child');
$obj2->set('innerObject', $obj3);

$this->assertTrue($obj->isDirty());

$obj2->save();
$this->assertFalse($obj->isDirty());

$obj3->set('key2', 'an unsaved value 2');
$this->assertTrue($obj->isDirty());


// test setting a child in child in child!
$obj4 = new ParseObject('TestClass');
$obj4->set('key2', 'child of child of child!');
$obj3->set('innerObject', $obj4);

$this->assertTrue($obj->isDirty());

$obj3->save();
$this->assertFalse($obj->isDirty());

$obj4->set('key2', 'an unsaved value 3');
$this->assertTrue($obj->isDirty());

$obj->destroy();
$obj2->destroy();
$obj3->destroy();

}

Expand Down
31 changes: 31 additions & 0 deletions tests/Parse/ParseRelationTest.php
Expand Up @@ -188,4 +188,35 @@ public function testSwitchingParent()
$this->assertEquals('child3', $children[0]->get('name'));

}

/**
* Verifies bi directional relations can be saved when an array of pointers is used and is in dirty state
* @author zeliard91
* @group bidir-test
*/
public function testBiDirectionalRelations()
{
Helper::clearClass('BiParent');
Helper::clearClass('BiChild');

$parent = new ParseObject('BiParent');

$child = new ParseObject('BiChild');
$child->set('name', 'Child');
$child->set('parent', $parent);

$child->save();
$parent->save();

$child2 = new ParseObject('BiChild');
$child2->set('name', 'Child 2');
$child2->set('parent', $parent);

$parent->setArray('children', [$child, $child2]);

$child2->save();
$parent->save();

}

}

0 comments on commit 66811f5

Please sign in to comment.