From f2aedeabfe66ea8b80f80f136f6d2eca95fe4837 Mon Sep 17 00:00:00 2001 From: Yar Kravtsov Date: Tue, 29 May 2018 14:07:15 +0300 Subject: [PATCH] Fixes a notice in count and countBy methods when aggregation result is empty --- .../AbstractCollectionRepository.php | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/PhalconRepositories/AbstractCollectionRepository.php b/src/PhalconRepositories/AbstractCollectionRepository.php index ba02475..a7b5e1b 100644 --- a/src/PhalconRepositories/AbstractCollectionRepository.php +++ b/src/PhalconRepositories/AbstractCollectionRepository.php @@ -224,7 +224,7 @@ public function getIn(string $whereInKey, array $whereIn = [], string $orderBy = return $this->model->find($input); } - + /** * Return the first ordered $limit records querying input parameters. * $limit = 0 means no limits. @@ -272,7 +272,7 @@ public function getInAndWhereByPage(int $page = 1, int $limit = 10, string $wher } return $this->model->find($input); - } + } /** * Return the first ordered $limit records querying input parameters. @@ -726,15 +726,8 @@ public function truncate() */ public function count() { - $aggregateArray = [ - [ - '$count' => 'number' - ] - ]; - $result = $this->model::aggregate($aggregateArray); - - return $result->toArray()[0]['number']; + return $this->countBy([]); } /** @@ -759,9 +752,13 @@ public function countBy(array $match = []) '$count' => 'number' ]; - $result = $this->model::aggregate($aggregateArray); + $result = $this->model::aggregate($aggregateArray)->toArray(); + + if (count($result)) { + return $result[0]['number']; + } - return $result->toArray()[0]['number']; + return 0; } /**