From b35827d1e2f0174d433d116a862a448815cc4fe1 Mon Sep 17 00:00:00 2001 From: Salem Ouerdani Date: Sat, 1 Jun 2019 00:34:03 +0100 Subject: [PATCH] [Bug] Fixes array count on object #10 --- src/Action.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Action.php b/src/Action.php index a6270e6..d683ed8 100644 --- a/src/Action.php +++ b/src/Action.php @@ -30,12 +30,12 @@ class Action extends \yii\rest\Action */ protected $relationName; /** - * @var string name of the attribute name used as a foreign key in the related model. also used to build the 'prefix'. + * @var string name of the attribute name used as a foreign key in the related model. also used to build the 'prefix'. * This should be provided by the UrlClass within queryParams. */ protected $linkAttribute; /** - * @var primary key value of the linkAttribute. + * @var primary key value of the linkAttribute. * This should be provided by the UrlClass within queryParams. * @see linkAttribute */ @@ -62,8 +62,9 @@ public function init() * Checks if the expected params that should be provided by the custom UrlClass are not missing. * @return Bolean. */ - protected function expectedParams($params) { - $expected = ['relativeClass','relationName','linkAttribute']; + protected function expectedParams($params) + { + $expected = ['relativeClass', 'relationName', 'linkAttribute']; foreach ($expected as $attr) { if (isset($params[$attr]) === false) return false; if ($attr === 'linkAttribute' && isset($params[$params[$attr]]) === false) return false; @@ -79,7 +80,7 @@ protected function expectedParams($params) { public function getRelativeModel() { $relativeClass = $this->relativeClass; - $relModel = $relativeClass::findOne($this->relative_id); + $relModel = $relativeClass::findOne($this->relative_id); if ($relModel === null) throw new NotFoundHttpException(StringHelper::basename($relativeClass) . " '$this->relative_id' not found."); @@ -106,12 +107,14 @@ public function findCurrentModels($IDs) $getter = 'get' . $this->relationName; $relModel = $this->getRelativeModel(); - $q = $relModel->$getter()->where([$pk => $ids]); - $model = count($ids) > 1 ? $q->all() : $q->one(); - if ($model === null or count($model) !== count($ids)) + $ci = count($ids); + $model = $ci > 1 ? $q->all() : $q->one(); + + if ($model === null || (is_array($model) && count($model) !== $ci)) { throw new NotFoundHttpException("Not found or unrelated objects."); + } return $model; }