Skip to content

Commit 9fb3ce5

Browse files
authored
Add batchSize to saveAll / destroyAll (#422)
1 parent 1c73a1c commit 9fb3ce5

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/Parse/ParseObject.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -797,16 +797,16 @@ public function destroy($useMasterKey = false)
797797
*
798798
* @param array $objects Objects to destroy.
799799
* @param bool $useMasterKey Whether to use the master key or not.
800+
* @param int $batchSize Number of objects to process per request
800801
*
801802
* @throws ParseAggregateException
802803
*/
803-
public static function destroyAll(array $objects, $useMasterKey = false)
804+
public static function destroyAll(array $objects, $useMasterKey = false, $batchSize = 40)
804805
{
805806
$errors = [];
806807
$objects = array_values($objects); // To support non-ordered arrays
807808
$count = count($objects);
808809
if ($count) {
809-
$batchSize = 40;
810810
$processed = 0;
811811
$currentBatch = [];
812812
$currentcount = 0;
@@ -1153,23 +1153,25 @@ public function save($useMasterKey = false)
11531153
*
11541154
* @param array $list
11551155
* @param bool $useMasterKey Whether to use the Master Key.
1156+
* @param int $batchSize Number of objects to process per request
11561157
*/
1157-
public static function saveAll($list, $useMasterKey = false)
1158+
public static function saveAll($list, $useMasterKey = false, $batchSize = 40)
11581159
{
1159-
static::deepSave($list, $useMasterKey);
1160+
static::deepSave($list, $useMasterKey, $batchSize);
11601161
}
11611162

11621163
/**
11631164
* Save object and unsaved children within.
11641165
*
11651166
* @param ParseObject|array $target
11661167
* @param bool $useMasterKey Whether to use the Master Key.
1168+
* @param int $batchSize Number of objects to process per request
11671169
*
11681170
* @throws Exception
11691171
* @throws ParseAggregateException
11701172
* @throws ParseException
11711173
*/
1172-
private static function deepSave($target, $useMasterKey = false)
1174+
private static function deepSave($target, $useMasterKey = false, $batchSize = 40)
11731175
{
11741176
$unsavedChildren = [];
11751177
$unsavedFiles = [];
@@ -1197,7 +1199,7 @@ private static function deepSave($target, $useMasterKey = false)
11971199
$newRemaining = [];
11981200

11991201
foreach ($remaining as $key => &$object) {
1200-
if (count($batch) > 40) {
1202+
if (count($batch) > $batchSize) {
12011203
$newRemaining[] = $object;
12021204
continue;
12031205
}

tests/Parse/ParseObjectTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,30 @@ public function testDestroyAll()
818818
$user->destroy(true);
819819
}
820820

821+
public function testBatchSize()
822+
{
823+
$batchSize = 1;
824+
Helper::clearClass('TestObject');
825+
826+
// log in
827+
$user = new ParseUser();
828+
$user->setUsername('username123');
829+
$user->setPassword('password123');
830+
$user->signUp();
831+
832+
$o1 = ParseObject::create('TestObject');
833+
$o2 = ParseObject::create('TestObject');
834+
$o3 = ParseObject::create('TestObject');
835+
ParseObject::saveAll([$o1, $o2, $o3], true, $batchSize);
836+
ParseObject::destroyAll([$o1, $o2, $o3], true, $batchSize);
837+
$query = new ParseQuery('TestObject');
838+
$results = $query->find();
839+
$this->assertEquals(0, count($results));
840+
841+
ParseUser::logOut();
842+
$user->destroy(true);
843+
}
844+
821845
public function testEmptyArray()
822846
{
823847
$obj = ParseObject::create('TestObject');

0 commit comments

Comments
 (0)