【发布时间】:2015-10-14 08:37:38
【问题描述】:
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:
堆栈跟踪:
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:
感谢各位的帮助。 Json 编码结果解决了这个问题。
public function actionGet_permissions() {
$result = Auth_Item::find()->where(['owner_user_id' => NULL])->all();
return Json::encode($result);
}
【问题讨论】:
-
您需要什么样的回应?通常控制器动作需要返回渲染视图的结果,但您也可以返回 json、xml 或其他文件格式,但是您需要进行一些配置。
-
呃,哇,是的,就是这样,Json::encode($result);成功了。 Incognito Skull 说了同样的话,但由于某种原因它没有完全点击。谢谢大家!
-
我几乎觉得我应该删除我在编辑 2 中发布的所有代码,这样它就不会分散人们对未来简单答案的注意力......你怎么看?
标签: activerecord yii2