Skip to content

Commit 66811f5

Browse files
author
Benjamin Wilson Friedman
authored
Merge pull request #296 from montymxb/bidir-fix
Fixes save issue with bi-directional relations via pointers
2 parents cb40054 + ba1bc60 commit 66811f5

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

src/Parse/ParseObject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ private function hasDirtyChildren()
279279
$this->estimatedData,
280280
function ($object) use (&$result) {
281281
if ($object instanceof ParseObject) {
282-
if ($object->isDirty()) {
282+
if ($object->_isDirty(false)) {
283283
$result = true;
284284
}
285285
}

tests/Parse/ParseObjectTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,9 @@ public function testGetAllKeys()
11491149

11501150
}
11511151

1152+
/**
1153+
* @group dirty-children
1154+
*/
11521155
public function testDirtyChildren()
11531156
{
11541157
$obj = new ParseObject('TestClass');
@@ -1161,10 +1164,51 @@ public function testDirtyChildren()
11611164
$this->assertFalse($obj->isDirty());
11621165

11631166
$obj->set('innerObject', $obj2);
1167+
$this->assertTrue($obj->isDirty());
1168+
1169+
$this->assertTrue($obj2->isDirty());
1170+
1171+
$obj->save();
1172+
$this->assertFalse($obj->isDirty());
1173+
$this->assertFalse($obj2->isDirty());
1174+
1175+
1176+
// update the child again
1177+
$obj2->set('key2', 'an unsaved value');
1178+
$this->assertTrue($obj->isDirty());
1179+
$obj->save();
1180+
1181+
1182+
// test setting a child in child
1183+
$obj3 = new ParseObject('TestClass');
1184+
$obj3->set('key2', 'child of child');
1185+
$obj2->set('innerObject', $obj3);
1186+
1187+
$this->assertTrue($obj->isDirty());
1188+
1189+
$obj2->save();
1190+
$this->assertFalse($obj->isDirty());
1191+
1192+
$obj3->set('key2', 'an unsaved value 2');
1193+
$this->assertTrue($obj->isDirty());
1194+
1195+
1196+
// test setting a child in child in child!
1197+
$obj4 = new ParseObject('TestClass');
1198+
$obj4->set('key2', 'child of child of child!');
1199+
$obj3->set('innerObject', $obj4);
1200+
1201+
$this->assertTrue($obj->isDirty());
1202+
1203+
$obj3->save();
1204+
$this->assertFalse($obj->isDirty());
11641205

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

11671209
$obj->destroy();
1210+
$obj2->destroy();
1211+
$obj3->destroy();
11681212

11691213
}
11701214

tests/Parse/ParseRelationTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,35 @@ public function testSwitchingParent()
188188
$this->assertEquals('child3', $children[0]->get('name'));
189189

190190
}
191+
192+
/**
193+
* Verifies bi directional relations can be saved when an array of pointers is used and is in dirty state
194+
* @author zeliard91
195+
* @group bidir-test
196+
*/
197+
public function testBiDirectionalRelations()
198+
{
199+
Helper::clearClass('BiParent');
200+
Helper::clearClass('BiChild');
201+
202+
$parent = new ParseObject('BiParent');
203+
204+
$child = new ParseObject('BiChild');
205+
$child->set('name', 'Child');
206+
$child->set('parent', $parent);
207+
208+
$child->save();
209+
$parent->save();
210+
211+
$child2 = new ParseObject('BiChild');
212+
$child2->set('name', 'Child 2');
213+
$child2->set('parent', $parent);
214+
215+
$parent->setArray('children', [$child, $child2]);
216+
217+
$child2->save();
218+
$parent->save();
219+
220+
}
221+
191222
}

0 commit comments

Comments
 (0)