Skip to content

Commit 3fc6766

Browse files
authored
ContainedBy Query (#418)
1 parent 1b99d91 commit 3fc6766

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/Parse/ParseQuery.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ public function endsWith($key, $value)
388388
return $this;
389389
}
390390

391-
/**
391+
/**
392392
* Adds a constraint for finding string values that contain a provided
393393
* string. This may be slow for large datasets.
394394
*
@@ -404,6 +404,22 @@ public function contains($key, $value)
404404
return $this;
405405
}
406406

407+
/**
408+
* Adds a constraint to the query that requires a particular key's value to
409+
* be contained by the provided list of values. Get objects where all array elements match.
410+
*
411+
* @param string $key The key to check.
412+
* @param mixed $value The values that will match.
413+
*
414+
* @return ParseQuery Returns this query, so you can chain this call.
415+
*/
416+
public function containedBy($key, $value)
417+
{
418+
$this->addCondition($key, '$containedBy', $value);
419+
420+
return $this;
421+
}
422+
407423
/**
408424
* Adds a constraint for finding string values that contain a provided
409425
* string using Full Text Search

tests/Parse/ParseQueryTest.php

+24-1
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,29 @@ public function testContainsAllDateArrayQueries()
12751275
);
12761276
}
12771277

1278+
public function testContainedByQuery()
1279+
{
1280+
Helper::clearClass('NumberSet');
1281+
$obj1 = ParseObject::create('TestObject');
1282+
$obj2 = ParseObject::create('TestObject');
1283+
$obj3 = ParseObject::create('TestObject');
1284+
$obj1->setArray('numbers', [0, 1, 2]);
1285+
$obj2->setArray('numbers', [2, 0]);
1286+
$obj3->setArray('numbers', [1, 2, 3, 4]);
1287+
$numberSet = [$obj1, $obj2, $obj3];
1288+
1289+
ParseObject::saveAll($numberSet);
1290+
1291+
$query = new ParseQuery('TestObject');
1292+
$query->containedBy('numbers', [1, 2, 3, 4, 5]);
1293+
$results = $query->find();
1294+
$this->assertEquals(
1295+
1,
1296+
count($results),
1297+
'Did not return correct number of objects.'
1298+
);
1299+
}
1300+
12781301
public function testContainsAllObjectArrayQueries()
12791302
{
12801303
Helper::clearClass('MessageSet');
@@ -2338,7 +2361,7 @@ public function testUnknownCondition()
23382361
'\Parse\ParseException',
23392362
'Unknown condition to set'
23402363
);
2341-
2364+
23422365
$query = new ParseQuery('TestObject');
23432366
$query->_setConditions([
23442367
'unrecognized' => 1

0 commit comments

Comments
 (0)