Skip to content

Commit b35827d

Browse files
committed
[Bug] Fixes array count on object tunecino#10
1 parent cd2c25c commit b35827d

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/Action.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ class Action extends \yii\rest\Action
3030
*/
3131
protected $relationName;
3232
/**
33-
* @var string name of the attribute name used as a foreign key in the related model. also used to build the 'prefix'.
33+
* @var string name of the attribute name used as a foreign key in the related model. also used to build the 'prefix'.
3434
* This should be provided by the UrlClass within queryParams.
3535
*/
3636
protected $linkAttribute;
3737
/**
38-
* @var primary key value of the linkAttribute.
38+
* @var primary key value of the linkAttribute.
3939
* This should be provided by the UrlClass within queryParams.
4040
* @see linkAttribute
4141
*/
@@ -62,8 +62,9 @@ public function init()
6262
* Checks if the expected params that should be provided by the custom UrlClass are not missing.
6363
* @return Bolean.
6464
*/
65-
protected function expectedParams($params) {
66-
$expected = ['relativeClass','relationName','linkAttribute'];
65+
protected function expectedParams($params)
66+
{
67+
$expected = ['relativeClass', 'relationName', 'linkAttribute'];
6768
foreach ($expected as $attr) {
6869
if (isset($params[$attr]) === false) return false;
6970
if ($attr === 'linkAttribute' && isset($params[$params[$attr]]) === false) return false;
@@ -79,7 +80,7 @@ protected function expectedParams($params) {
7980
public function getRelativeModel()
8081
{
8182
$relativeClass = $this->relativeClass;
82-
$relModel = $relativeClass::findOne($this->relative_id);
83+
$relModel = $relativeClass::findOne($this->relative_id);
8384

8485
if ($relModel === null)
8586
throw new NotFoundHttpException(StringHelper::basename($relativeClass) . " '$this->relative_id' not found.");
@@ -106,12 +107,14 @@ public function findCurrentModels($IDs)
106107
$getter = 'get' . $this->relationName;
107108

108109
$relModel = $this->getRelativeModel();
109-
110110
$q = $relModel->$getter()->where([$pk => $ids]);
111-
$model = count($ids) > 1 ? $q->all() : $q->one();
112111

113-
if ($model === null or count($model) !== count($ids))
112+
$ci = count($ids);
113+
$model = $ci > 1 ? $q->all() : $q->one();
114+
115+
if ($model === null || (is_array($model) && count($model) !== $ci)) {
114116
throw new NotFoundHttpException("Not found or unrelated objects.");
117+
}
115118

116119
return $model;
117120
}

0 commit comments

Comments
 (0)