thinkphp5内报错Call to a member function toArray() on array
使用场景
使用模型查询后,想获取不包含对象内容的数组结构的结果。
使用方式
$gardenAuth = new GardenAuth();
$res = $gardenAuth->where(['id'=>$id])->select()->toArray();
结果
Call to a member function toArray() on array
原因
数据库(database.php)里设置的返回数据结果为数组类型
'resultset_type' => 'array'
解决方式
方法一
全局:改为 'resultset_type' => 'collection'
方法二:
局部:在模型中加入
protected $resultSetType = 'collection';
探讨了在ThinkPHP5框架中使用模型查询时遇到的CalltoamemberfunctiontoArray()onarray错误,分析了错误产生的原因,即数据库配置中resultset_type设置为array,并提供了两种解决方案:一是全局修改配置为collection;二是局部修改模型中的resultSetType属性。
525

被折叠的 条评论
为什么被折叠?



