yii2 ActiveRecord findBySql-响应内容不能为数组错误 [英] yii2 ActiveRecord findBySql - Response content must not be an array Error
问题描述
Yii2细微差别的新手。
New to Yii2 nuances.
只是尝试从ActiveRecord查询中获得收益。我意识到,使用Yii2约定可能更容易做到这一点
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();
}
- 错误响应内容不能为数组。
我认为它很明显是一组简单的记录我正在尝试使用此功能返回。非常感谢您提供任何帮助,并让我知道是否还有其他信息可以帮助您。
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.
为什么不允许findBySql返回数组?我知道我在这里缺少简单的东西。
Why would findBySql not be allowed to return an array? I know I'm missing something simple here.
谢谢!
编辑1:
Auth_Item::find()->where(['owner_user_id' => null])->all();
返回相同的错误。再次,这似乎是一个简单的查询。
Returns the same error. And again, this seems like such a simple query.
编辑2:
堆栈跟踪:
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:
感谢帮助人员。
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 ActiveRecord findBySql-响应内容不能为数组错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!