From 583e5b44101f796db831afc2cb1fa3e14ceaa761 Mon Sep 17 00:00:00 2001 From: Dirk Scholten Date: Thu, 6 Jun 2019 15:03:15 +0200 Subject: [PATCH] findBy can now return as array optionally --- Mapper.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Mapper.php b/Mapper.php index 4ba76c3..aa349e6 100644 --- a/Mapper.php +++ b/Mapper.php @@ -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) @@ -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); } @@ -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);