-
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
Changes from 22 commits
2641166
879f794
9dc0031
90d5043
e39e1eb
54ea7bf
478096b
1238708
62793f0
c6f6651
b633d20
c21ee68
15cc005
9bb45bf
69958d0
44443f6
a961cb3
b0240de
239099d
c8b6ce7
92596a2
fd3e7a4
0bc7451
6f59192
6eb5fee
4d82517
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
namespace NYPL\Starter\Model\ModelTrait; | ||
|
||
use NYPL\Starter\APIException; | ||
use NYPL\Starter\APILogger; | ||
use NYPL\Starter\DB; | ||
use NYPL\Starter\Filter; | ||
use NYPL\Starter\Filter\OrFilter; | ||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. Remove checking for |
||
$this->obtainTotalCount(); | ||
} | ||
|
||
$className = get_class($this->getBaseModel()); | ||
|
||
foreach ($selectStatement->fetchAll() as $result) { | ||
|
@@ -336,6 +341,30 @@ protected function addOrderBy(SelectStatement $selectStatement) | |
return true; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
protected function obtainTotalCount() | ||
{ | ||
/** | ||
* @var DBTrait $baseModel | ||
*/ | ||
$baseModel = $this->getBaseModel(); | ||
|
||
$selectStatement = DB::getDatabase()->select(['id']) | ||
->from($baseModel->translateDbName($baseModel->getTableName())) | ||
->groupBy('id'); | ||
if ($this->getFilters()) { | ||
$this->applyFilters($this->getFilters(), $selectStatement); | ||
} | ||
|
||
$selectStatement = $selectStatement->count('id')->execute(); | ||
|
||
$this->setTotalCount($selectStatement->rowCount()); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @param bool $ignoreNoRecord | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
use NYPL\Starter\Model; | ||
use NYPL\Starter\Model\Response; | ||
use NYPL\Starter\ModelSet; | ||
|
||
abstract class SuccessResponse extends Response | ||
{ | ||
|
@@ -16,13 +17,18 @@ abstract class SuccessResponse extends Response | |
*/ | ||
public $count = 0; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
public $totalCount; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
public $statusCode; | ||
|
||
/** | ||
* @param Model|Model[] $model | ||
* @param Model|Model[]|ModelSet $model | ||
* @param int $code | ||
*/ | ||
public function __construct($model = null, $code = 200) | ||
|
@@ -35,16 +41,46 @@ public function __construct($model = null, $code = 200) | |
} | ||
|
||
/** | ||
* @param Model|Model[] $model | ||
* @param Model|Model[]|ModelSet $model | ||
*/ | ||
public function initializeResponse($model) | ||
{ | ||
if (is_array($model)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactor out |
||
$this->initializeArrayOfModels($model); | ||
} else if ($model instanceof ModelSet) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe move |
||
$this->initializeModelSet($model); | ||
} else if ($model instanceof Model) { | ||
$this->initializeModel($model); | ||
} | ||
} | ||
|
||
/** | ||
* @param Model $model | ||
*/ | ||
public function initializeModel($model) | ||
{ | ||
$this->setData($model); | ||
$this->setCount(1); | ||
} | ||
|
||
if (is_array($model)) { | ||
$this->setCount(count($model)); | ||
} else { | ||
$this->setCount(1); | ||
/** | ||
* @param Model[] $arrayOfModels | ||
*/ | ||
public function initializeArrayOfModels($arrayOfModels) | ||
{ | ||
$this->setData($arrayOfModels); | ||
$this->setCount(count($arrayOfModels)); | ||
} | ||
|
||
/** | ||
* @param ModelSet $modelSet | ||
*/ | ||
public function initializeModelSet($modelSet) | ||
{ | ||
$this->setData($modelSet->getData()); | ||
$this->setCount(count($modelSet->getData())); | ||
if ($modelSet->isIncludeTotalCount()) { | ||
$this->setTotalCount($modelSet->getTotalCount()); | ||
} | ||
} | ||
|
||
|
@@ -95,4 +131,20 @@ public function setCount($count) | |
{ | ||
$this->count = $count; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getTotalCount() | ||
{ | ||
return $this->totalCount; | ||
} | ||
|
||
/** | ||
* @param int $totalCount | ||
*/ | ||
public function setTotalCount($totalCount) | ||
{ | ||
$this->totalCount = $totalCount; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,16 @@ class ModelSet extends Model implements ReadInterface | |
*/ | ||
public $limit = 25; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
public $totalCount = 0; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
public $includeTotalCount = false; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
|
@@ -175,4 +185,36 @@ public function setNoDefaultSorting($noDefaultSorting) | |
{ | ||
$this->noDefaultSorting = (bool) $noDefaultSorting; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isIncludeTotalCount() | ||
{ | ||
return $this->includeTotalCount; | ||
} | ||
|
||
/** | ||
* @param bool $includeTotalCount | ||
*/ | ||
public function setIncludeTotalCount($includeTotalCount) | ||
{ | ||
$this->includeTotalCount = $includeTotalCount; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe typecast the $includeTotalCount to |
||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getTotalCount() | ||
{ | ||
return $this->totalCount; | ||
} | ||
|
||
/** | ||
* @param int $totalCount | ||
*/ | ||
public function setTotalCount($totalCount = 0) | ||
{ | ||
$this->totalCount = $totalCount; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe typecast |
||
} | ||
} |
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