【问题标题】:yii2 ActiveRecord findBySql - Response content must not be an array Erroryii2 ActiveRecord findBySql - 响应内容不能是数组错误
【发布时间】: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


【解决方案1】:

使用Active Record:

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

【讨论】:

  • 'owner_user_id' 必须是桌子上 PK 的一部分吗?
  • 不..你的反应是什么?例如。 json 或其他。
  • 它在那里,当我直接在 phpmyadmin 中查询数据库时,结果集就很好了。 ://
  • 使用查询的活动记录和粘贴结果在任何模型或控制器中运行查询。
  • 添加 Json::encode($result);你的回答,我可以这样标记它
【解决方案2】:

您应该使用 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

【讨论】:

  • 这是否需要我在每次请求/响应之前以这种方式设置格式?因此,我必须返回并更改所有返回 HTML 的请求并在发出请求之前更改响应格式?当我想要 JSON 时也一样?
【解决方案3】:

更好的是您直接在配置文件中进行更改,以便每个请求都获得正确的 json 编码。

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

【讨论】:

    【解决方案4】:

    format json 通过转换为 json 使变量 post 过程自动标量,而 html 响应类型不会将数组转换为标量,因此数组必须具有 json 响应,我不小心将响应类型更改为 HTML,但忘记摆脱特定位置的数组结构

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      • 2017-10-21
      • 2017-05-07
      • 2019-11-04
      • 2018-10-19
      • 1970-01-01
      • 2015-12-02
      相关资源
      最近更新 更多