yii2 ActiveRecord findBySql - 响应内容不能是数组错误 [英] yii2 ActiveRecord findBySql - Response content must not be an array Error

查看:15
本文介绍了yii2 ActiveRecord findBySql - 响应内容不能是数组错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚接触 Yii2 的细微差别.

只是想从 ActiveRecord 查询中获取返回值.我意识到使用 Yii2 约定可能有更简单的方法来做到这一点

公共函数 actionGet_permissions() {$sql = 'select * from auth_item where owner_user_id IS NULL';返回 Auth_Item::findBySql($sql)->all();}

<块引用>

  • 错误响应内容不能是数组."

我认为我试图用这个函数返回的简单记录集非常明显.非常感谢您的帮助,如果有其他信息会有所帮助,请告诉我.

为什么不允许 findBySql 返回数组?我知道我在这里遗漏了一些简单的东西.

谢谢!

编辑 1:

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

返回相同的错误.再说一次,这似乎是一个如此简单的查询.

编辑 2:

堆栈跟踪:

无效参数 – yiiaseInvalidParamException响应内容不能是数组.• 1. 在 C:xampphtdocsclientivendoryiisoftyii2webResponse.php 中的第 944 行throw new InvalidConfigException("'{$this->format}' 响应格式器无效.它必须实现 ResponseFormatterInterface.");}} elseif ($this->format === self::FORMAT_RAW) {$this->content = $this->data;} 别的 {throw new InvalidConfigException("不支持的响应格式:{$this->format}");}如果 (is_array($this->content)) {throw new InvalidParamException("响应内容不能是数组.");} elseif (is_object($this->content)) {if (method_exists($this->content, '__toString')) {$this->content = $this->content->__toString();} 别的 {throw new InvalidParamException("响应内容必须是字符串或实现__toString()的对象.");}}}}• 2. 在 C:xampphtdocsclivendoryiisoftyii2webResponse.php 中 – yiiwebResponse::prepare() 在第 312 行• 3. 在 C:xampphtdocsclivendoryiisoftyii2aseApplication.php 中 – yiiwebResponse::send() 在第 381 行• 4. 在 C:xampphtdocsclifrontendwebindex.php – yiiaseApplication::run() 第 18 行

编辑 3:

感谢大家的帮助.Json 编码结果修复了问题.

公共函数 actionGet_permissions() {$result = Auth_Item::find()->where(['owner_user_id' => NULL])->all();返回 Json::encode($result);}

解决方案

Use Active Record:

公共函数 actionGet_permissions() {$result = Auth_Item::find()->where(['owner_user_id' => NULL])->all();返回 Json::encode($result);}

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 – yiiaseInvalidParamException
Response content must not be an array.
•   1. in C:xampphtdocsclientivendoryiisoftyii2webResponse.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:xampphtdocsclivendoryiisoftyii2webResponse.php – yiiwebResponse::prepare() at line 312 
•   3. in C:xampphtdocsclivendoryiisoftyii2aseApplication.php – yiiwebResponse::send() at line 381 
•   4. in C:xampphtdocsclifrontendwebindex.php – yiiaseApplication::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);
}

解决方案

Use Active Record:

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

这篇关于yii2 ActiveRecord findBySql - 响应内容不能是数组错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆