From 0299632dea45d03a04bd2cf35a64eaf33ebc0119 Mon Sep 17 00:00:00 2001 From: Adeyemi Olaoye Date: Tue, 20 Jun 2017 15:44:15 +0100 Subject: [PATCH 1/3] Fix: Fix Get and Find Active methods --- src/Model/BaseModel.php | 43 +++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/src/Model/BaseModel.php b/src/Model/BaseModel.php index 7062914..9c36c24 100644 --- a/src/Model/BaseModel.php +++ b/src/Model/BaseModel.php @@ -127,30 +127,33 @@ public static function getTotalCount($countField = null) * Gets all active * @author Adegoke Obasa * @author Olawale Lawal + * @author Adeyemi Olaoye * @param array $orderBy - * @return array|\yii\db\ActiveRecord[] + * @param string $activeColumn + * @param int $activeValue + * @return array|ActiveRecord[] */ - public static function getActive($orderBy = []) + public static function getActive($orderBy = [], $activeColumn = 'is_active', $activeValue = 1) { - $model = get_called_class(); - $model = new $model; - return self::find() - ->asArray() - ->orderBy($orderBy) - ->all(); + return self::findActive($orderBy, $activeColumn, $activeValue)->all(); } /** * Gets all active * @author Adeyemi Olaoye - * @return array|\yii\db\ActiveRecord[] + * @param array $orderBy + * @param string $activeColumn + * @param int $activeValue + * @return ActiveQuery */ - public static function findActive() + public static function findActive($orderBy = [], $activeColumn = 'is_active', $activeValue = 1) { + /** @var self $model */ $model = get_called_class(); - $model = new $model; - return self::find() - ->all(); + return $model::find() + ->where([$activeColumn => $activeValue]) + ->asArray() + ->orderBy($orderBy); } @@ -172,16 +175,22 @@ public static function getIdByField($field, $value) * @author Olawale Lawal * @param $startDate * @param $endDate + * @param string $createdAtColumn * @return array|ActiveQuery */ - public static function getByCreatedDateRange($startDate, $endDate) - { + public static function getByCreatedDateRange( + $startDate, + $endDate, + $createdAtColumn = 'created_at' + ) { $model = get_called_class(); $model = new $model; return self::find() - ->andWhere($model::tableName() . '.created_at BETWEEN :start_date AND :end_date') - ->params(['start_date' => $startDate, 'end_date' => $endDate]); + ->andWhere( + $model::tableName() . '.' . $createdAtColumn . ' BETWEEN :start_date AND :end_date', + ['start_date' => $startDate, 'end_date' => $endDate] + ); } From 36da0737c4040a3d444034a154f3f5c64690d2c9 Mon Sep 17 00:00:00 2001 From: Adeyemi Olaoye Date: Tue, 20 Jun 2017 16:46:29 +0100 Subject: [PATCH 2/3] Refactor: Fix PHP CS issues --- src/Action/UpdateAction.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Action/UpdateAction.php b/src/Action/UpdateAction.php index 8d00641..4050577 100644 --- a/src/Action/UpdateAction.php +++ b/src/Action/UpdateAction.php @@ -47,11 +47,9 @@ public function run() if (!$this->model->save()) { return $controller->returnError($this->model->getErrors()); } - } catch (IntegrityException $ex) { return $controller->returnError($this->integrityExceptionMessage); } return $controller->returnSuccess($this->successMessage); } } - From 2311bb4d8cc94528958c61b7512a3c8cca6e6aed Mon Sep 17 00:00:00 2001 From: Adeyemi Olaoye Date: Tue, 20 Jun 2017 16:55:15 +0100 Subject: [PATCH 3/3] Fix: Remove asArray --- src/Model/BaseModel.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Model/BaseModel.php b/src/Model/BaseModel.php index 9c36c24..9d862f2 100644 --- a/src/Model/BaseModel.php +++ b/src/Model/BaseModel.php @@ -152,7 +152,6 @@ public static function findActive($orderBy = [], $activeColumn = 'is_active', $a $model = get_called_class(); return $model::find() ->where([$activeColumn => $activeValue]) - ->asArray() ->orderBy($orderBy); }