Skip to content

Commit

Permalink
Fixes a notice in count and countBy methods when aggregation result i…
Browse files Browse the repository at this point in the history
…s empty
  • Loading branch information
yarlson committed May 29, 2018
1 parent 4912dc7 commit f2aedea
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions src/PhalconRepositories/AbstractCollectionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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([]);
}

/**
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit f2aedea

Please sign in to comment.