yii2 ActiveRecord findBySql - Response content must not be an array Error

爱⌒轻易说出口 提交于 2019-12-21 17:37:01

问题


New to Yii2 nuances.

Just trying to get a return from a ActiveRecord query. I realize there is probably a much easier 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 "Response content must not be an array."

I think its pretty obvious the simple set of records I'm trying to return with this function. Any help is much appreciated, and let me know if any other information will help.

Why would findBySql not be allowed to return an array? I know I'm missing something simple here.

Thanks!

Edit 1:

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

Returns the same error. And again, this seems like such a simple query.

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 312 
•   3. in C:\xampp\htdocs\cli\vendor\yiisoft\yii2\base\Application.php – yii\web\Response::send() at line 381 
•   4. in C:\xampp\htdocs\cli\frontend\web\index.php – yii\base\Application::run() at line 18 

Edit 3:

Thanks for the help guys. Json encoding the result fixed the issue.

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

回答1:


Use Active Record:

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



回答2:


You should use Yii2 features and modify response format.

Default response format is HTML, that's why you have the following error :

Response content must not be an array

You should simply try this :

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

Yii will automatically send http headers (previous answer will not) and encode your models.

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

And if you want to use this response format in all your controllers, you could modify response config :

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

Read more about yii\web\Response




回答3:


Better You make changes directly into your configuration file so that every request gets the proper json encoding.

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



回答4:


format json makes the variable post process automatically scalar by converting to json, whereas html response type does not convert arrays to scalars, therefore arrays have to have json response, I accidentally changed response type to HTML, but did forget to get rid of array structure at a particular place



来源:https://stackoverflow.com/questions/33120470/yii2-activerecord-findbysql-response-content-must-not-be-an-array-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!