Skip to content

Commit

Permalink
findBy can now return as array optionally
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirk Scholten committed Jun 6, 2019
1 parent 5bdbc0c commit 583e5b4
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,11 @@ public function find($model, $id, $related = 1)
* @param array $order Order to sort by as key/value pairs
* @param int $related Number of levels of related records to include
* @param bool $deleted Whether to include deleted records
* @param bool $returnAsArray Whether to return as array
* @return MappedRecordIterator
*/
public function findBy($model, array $criteria, array $order = array(),
$related = 1, $deleted = false)
$related = 1, $deleted = false, $returnAsArray = false)
{
$query = $this->getQuerySelectPart($model, $related)
. $this->getQueryWherePart($criteria, $model)
Expand All @@ -192,6 +193,9 @@ public function findBy($model, array $criteria, array $order = array(),
$result = $this->client->query($query);
}

if($returnAsArray) {
return iterator_to_array(new MappedRecordIterator($result, $this, $model));
}
return new MappedRecordIterator($result, $this, $model);
}

Expand Down Expand Up @@ -553,14 +557,14 @@ private function getQueryWherePart(array $criteria, $model)
if (!$field) {
throw new \InvalidArgumentException('Invalid field ' . $name);
}

if (is_array($value)) {
$quotedValueList = array();

foreach ($value as $v) {
$quotedValueList[] = $this->getQuotedWhereValue($field, $v, $objectDescription);
}

$quotedValue = '(' . implode(',', $quotedValueList) . ')';
} else {
$quotedValue = $this->getQuotedWhereValue($field, $value, $objectDescription);
Expand Down

0 comments on commit 583e5b4

Please sign in to comment.