Yii2 ActiveRecord findBySql - The content of the response should not be an array Error

New to the nuances of Yii2.

Just trying to get a return from an ActiveRecord request. I understand that there is probably a much simpler way to do this using Yii2 conventions

public function actionGet_permissions() {

    $sql = 'select * from auth_item where owner_user_id IS NULL';
    return Auth_Item::findBySql($sql)->all();
}
  • Errors "The content of the response should not be an array."

I think this is a pretty obvious simple set of records I'm trying to return using this function. Any help is greatly appreciated and let me know if any other information will help.

Why won't findBySql be allowed to return an array? I know that I am missing something simple here.

Thanks!

Change 1:

Auth_Item::find()->where(['owner_user_id' => null])->all();

It returns the same error. Again, this seems like such a simple request.

Edit 2:

Stack trace:

Invalid Parameter – yii\base\InvalidParamException
Response content must not be an array.1. in C:\xampp\htdocs\clienti\vendor\yiisoft\yii2\web\Response.php at line 944 
            throw new InvalidConfigException("The '{$this->format}' response formatter is invalid. It must implement the ResponseFormatterInterface.");
        }
    } elseif ($this->format === self::FORMAT_RAW) {
        $this->content = $this->data;
    } else {
        throw new InvalidConfigException("Unsupported response format: {$this->format}");
    }

    if (is_array($this->content)) {
        throw new InvalidParamException("Response content must not be an array.");
    } elseif (is_object($this->content)) {
        if (method_exists($this->content, '__toString')) {
            $this->content = $this->content->__toString();
        } else {
            throw new InvalidParamException("Response content must be a string or an object implementing __toString().");
        }
    }
}
}2. in C:\xampp\htdocs\cli\vendor\yiisoft\yii2\web\Response.php – yii\web\Response::prepare() at line 3123. in C:\xampp\htdocs\cli\vendor\yiisoft\yii2\base\Application.php – yii\web\Response::send() at line 3814. in C:\xampp\htdocs\cli\frontend\web\index.php – yii\base\Application::run() at line 18 

Edit 3:

. Json, , .

public function actionGet_permissions() {
    $result = Auth_Item::find()->where(['owner_user_id' => NULL])->all();
    return Json::encode($result);
}
+5
4

Active Record:

public function actionGet_permissions() {
   $result = Auth_Item::find()->where(['owner_user_id' => NULL])->all();
   return Json::encode($result);
}
+1

Yii2 .

- HTML, :

:

public function actionGet_permissions()
{
    \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
    return Auth_Item::find()->where(['owner_user_id' => NULL])->all();
}

Yii http ( ) .

: http://www.yiiframework.com/doc-2.0/guide-runtime-responses.html

, :

'response' => [                 
    'format' => yii\web\Response::FORMAT_JSON, 
    'charset' => 'UTF-8',               
],

yii\web\Response

+4

, json.

'response' => [                 
'format' => yii\web\Response::FORMAT_JSON, 
                'charset' => 'UTF-8',               
 ],
0
source

The json format makes the post-process variable automatically scalar by converting to json, while the html type of the response does not convert arrays to scalars, so arrays should have a json response, I accidentally changed the type of response to HTML, but forgot to get rid of the structure of the array in specific place

0
source

Source: https://habr.com/ru/post/1611563/


All Articles