-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Show total count #13
base: master
Are you sure you want to change the base?
Show total count #13
Conversation
src/Controller.php
Outdated
@@ -249,6 +249,10 @@ protected function getDefaultReadResponse( | |||
|
|||
$model->setLimit($this->getRequest()->getParam('limit')); | |||
|
|||
if ($this->getRequest()->getParam('include_count') == 'true') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to do strict checking ===
…delSet. Clearly assign boolean value to .
…SelectStatement::count() aggregation feature, avoids returning full data set which can potentially crash API endpoint.
@kfriedman, ready for review 🙂 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! Looks very solid to me.
src/Model/ModelTrait/DBReadTrait.php
Outdated
*/ | ||
$baseModel = $this->getBaseModel(); | ||
|
||
$selectStatement = DB::getDatabase()->select() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Investigate using count
statement: https://github.com/FaaPz/Slim-PDO/blob/master/docs/Statement/SELECT.md#countcolumn---as--null-distinct--false
src/Model/ModelTrait/DBReadTrait.php
Outdated
@@ -289,6 +290,10 @@ protected function setSet($ignoreNoRecord = false) | |||
} | |||
|
|||
if ($selectStatement->rowCount()) { | |||
if ($this->isIncludeTotalCount() == true) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try to use strict checking
@@ -289,6 +290,10 @@ protected function setSet($ignoreNoRecord = false) | |||
} | |||
|
|||
if ($selectStatement->rowCount()) { | |||
if ($this->isIncludeTotalCount() === true) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove checking for true
src/Controller.php
Outdated
@@ -249,6 +249,12 @@ protected function getDefaultReadResponse( | |||
|
|||
$model->setLimit($this->getRequest()->getParam('limit')); | |||
|
|||
$includeTotalCount = $this->getRequest()->getParam('includeTotalCount') === 'true' ? true : false ; | |||
|
|||
if ($includeTotalCount === true) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can remove checking for true
here if you're sure it's going to be boolean
*/ | ||
public function initializeResponse($model) | ||
{ | ||
if (is_array($model)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactor out else
s; you can just use return
statements
{ | ||
if (is_array($model)) { | ||
$this->initializeArrayOfModels($model); | ||
} else if ($model instanceof ModelSet) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe move ModelSet
to end to indicate it's the newest functionality?
src/ModelSet.php
Outdated
*/ | ||
public function setIncludeTotalCount($includeTotalCount) | ||
{ | ||
$this->includeTotalCount = $includeTotalCount; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe typecast the $includeTotalCount to (bool)
?
src/ModelSet.php
Outdated
*/ | ||
public function setTotalCount($totalCount = 0) | ||
{ | ||
$this->totalCount = $totalCount; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe typecast $totalCount
to (int)
?
…n instead. Checking for ModelSet first to avoid returning baseModel in response.
Let's find a time to talk about this? Thanks!