Skip to content

Commit

Permalink
[Bug] Fixes array count on object #10
Browse files Browse the repository at this point in the history
  • Loading branch information
tunecino committed May 31, 2019
1 parent cd2c25c commit b35827d
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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;
Expand All @@ -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.");
Expand All @@ -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;
}
Expand Down

0 comments on commit b35827d

Please sign in to comment.