yii2 ActiveRecord findBySql-响应内容不能是数组错误

yii2 ActiveRecord findBySql-响应内容不能是数组错误,activerecord,yii2,Activerecord,Yii2,Yii2细微差别的新特点 只是尝试从ActiveRecord查询中获取返回。我意识到使用Yii2约定可能有一种更简单的方法来实现这一点 public function actionGet_permissions() { $sql = 'select * from auth_item where owner_user_id IS NULL'; return Auth_Item::findBySql($sql)->all(); } “错误”响应内容不能是数组 我认为很明

Yii2细微差别的新特点

只是尝试从ActiveRecord查询中获取返回。我意识到使用Yii2约定可能有一种更简单的方法来实现这一点

public function actionGet_permissions() {

    $sql = 'select * from auth_item where owner_user_id IS NULL';
    return Auth_Item::findBySql($sql)->all();
}
  • “错误”响应内容不能是数组
我认为很明显,我试图用这个函数返回一组简单的记录。非常感谢您提供的任何帮助,如果有其他信息,请告诉我

为什么不允许findBySql返回数组?我知道我错过了一些简单的东西

谢谢

编辑1:

Auth_Item::find()->where(['owner_user_id' => null])->all();
返回相同的错误。同样,这似乎是一个简单的查询

编辑2:

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

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 
编辑3:

Auth_Item::find()->where(['owner_user_id' => null])->all();
谢谢你们的帮助。Json编码结果解决了这个问题

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

使用活动记录:

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

您应该使用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头(前面的答案不会),并对您的模型进行编码

阅读更多:

如果要在所有控制器中使用此响应格式,可以修改响应配置:

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

阅读更多关于

的信息,更好地直接更改配置文件,以便每个请求都获得正确的json编码

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

格式json通过转换为json使变量post process自动标量化,而html响应类型不会将数组转换为标量,因此数组必须有json响应,我不小心将响应类型更改为html,但是你忘了在某个特定的位置去掉数组结构了吗

owner\u user\u id必须是桌面PK的一部分吗?不,你的回答是什么?例如,当我在phpmyadmin中直接查询数据库时,结果集返回得很好:/使用活动记录在任何模型或控制器中运行查询并粘贴查询结果;对你的回答,我可以记下来。你需要什么样的回答?通常,控制器操作需要从呈现视图返回结果,但是您也可以返回json、xml或其他文件格式,但是您需要进行一些配置才能这样做;成功了。“隐姓埋名的骷髅”也说了同样的话,但由于某种原因,它并没有完全合拍。谢谢大家!我几乎觉得我应该删除我在编辑2中发布的所有代码,这样就不会分散人们对未来简单答案的注意力。。。您认为如何?这是否需要我在每次请求/响应之前以这种方式设置格式?因此,我必须返回并更改所有返回HTML的请求,并在发出请求之前更改响应格式?当我需要JSON时也是这样?