thinkphp5.1moxingselectfanhuiduixiangzhuanshuzu,thinkphp5.1模型对象返回对象的形式,转成数组只需要修改database.php中的配置:'resultset_type' => 'array',array修改为:collection即可。
thinkphp5.1模型对象返回对象的形式,转成数组只需要修改database.php中的配置:'resultset_type' => 'array',array修改为:collection即可。
object(think\model\Collection)#42 (1) {
["items":protected]=>
array(1) {
[0]=>
object(app\index\model\Admin)#40 (2) {
["data"]=>
array(18) {
["id"]=>
int(1)
["is_admin"]=>
int(1)
["username"]=>
string(5) "admin"
["fullname"]=>
string(13) "administrator"
["phone"]=>
string(11) "15887288437"
["password_reset_token"]=>
string(32) "98641e30ace18a76f07bd7e5fbf7cd6c"
["access_token"]=>
string(32) "43641e30ace18a74207b27e3f3f3d6ff"
["email"]=>
string(12) "admin@qq.com"
["password"]=>
string(32) "98641e30ace18a76f07bd7e5fbf7cd6c"
["login_times"]=>
int(5)
["login_ip"]=>
string(9) "127.0.0.1"
["login_time"]=>
int(1608477052)
["last_login_ip"]=>
string(9) "127.0.0.1"
["last_login_time"]=>
int(1608477052)
["user_agent"]=>
string(0) ""
["create_time"]=>
int(1608477052)
["update_time"]=>
int(1608477052)
["status"]=>
int(1)
}
["relation"]=>
array(0) {
}
}
}
}
可能你修改了查询出来的记录还是对象形式,所以还需在select()后追加->toArray(),比如:select()->toArray();
var_dump((new Admin())->select()->toArray());
array(1) {
[0]=>
array(18) {
["id"]=>
int(1)
["is_admin"]=>
int(1)
["username"]=>
string(5) "admin"
["fullname"]=>
string(13) "administrator"
["phone"]=>
string(11) "15887288437"
["password_reset_token"]=>
string(32) "98641e30ace18a76f07bd7e5fbf7cd6c"
["access_token"]=>
string(32) "43641e30ace18a74207b27e3f3f3d6ff"
["email"]=>
string(12) "admin@qq.com"
["password"]=>
string(32) "98641e30ace18a76f07bd7e5fbf7cd6c"
["login_times"]=>
int(5)
["login_ip"]=>
string(9) "127.0.0.1"
["login_time"]=>
int(1608477052)
["last_login_ip"]=>
string(9) "127.0.0.1"
["last_login_time"]=>
int(1608477052)
["user_agent"]=>
string(0) ""
["create_time"]=>
int(1608477052)
["update_time"]=>
int(1608477052)
["status"]=>
int(1)
}
}
如果不修改resultset_type的配置为:collection,而是resultset_type=>"array"的情况,在使用:
// table方法必须指定完整的数据表名
$result = Db::table('tp5_admin')->select();
var_dump($result);
返回的即为数组形式:
array(1) {
[0]=>
array(18) {
["id"]=>
int(1)
["is_admin"]=>
int(1)
["username"]=>
string(5) "admin"
["fullname"]=>
string(13) "administrator"
["phone"]=>
string(11) "15887288437"
["password_reset_token"]=>
string(32) "98641e30ace18a76f07bd7e5fbf7cd6c"
["access_token"]=>
string(32) "43641e30ace18a74207b27e3f3f3d6ff"
["email"]=>
string(12) "admin@qq.com"
["password"]=>
string(32) "98641e30ace18a76f07bd7e5fbf7cd6c"
["login_times"]=>
int(5)
["login_ip"]=>
string(9) "127.0.0.1"
["login_time"]=>
int(1608477052)
["last_login_ip"]=>
string(9) "127.0.0.1"
["last_login_time"]=>
int(1608477052)
["user_agent"]=>
string(0) ""
["create_time"]=>
int(1608477052)
["update_time"]=>
int(1608477052)
["status"]=>
int(1)
}
}
进过测试,如果'resultset_type' => 'collection',的情况下,Db类返回的就是对象形式,要返回array形式,也许要在select()后使用->toArray();的形式。
// table方法必须指定完整的数据表名
$result = Db::table('tp5_admin')->select();
var_dump($result);
object(think\Collection)#39 (1) {
["items":protected]=>
array(1) {
[0]=>
array(18) {
["id"]=>
int(1)
["is_admin"]=>
int(1)
["username"]=>
string(5) "admin"
["fullname"]=>
string(13) "administrator"
["phone"]=>
string(11) "15887288437"
["password_reset_token"]=>
string(32) "98641e30ace18a76f07bd7e5fbf7cd6c"
["access_token"]=>
string(32) "43641e30ace18a74207b27e3f3f3d6ff"
["email"]=>
string(12) "admin@qq.com"
["password"]=>
string(32) "98641e30ace18a76f07bd7e5fbf7cd6c"
["login_times"]=>
int(5)
["login_ip"]=>
string(9) "127.0.0.1"
["login_time"]=>
int(1608477052)
["last_login_ip"]=>
string(9) "127.0.0.1"
["last_login_time"]=>
int(1608477052)
["user_agent"]=>
string(0) ""
["create_time"]=>
int(1608477052)
["update_time"]=>
int(1608477052)
["status"]=>
int(1)
}
}
}
此时在select()后添加:->toArray();如下:
array(1) {
[0]=>
array(18) {
["id"]=>
int(1)
["is_admin"]=>
int(1)
["username"]=>
string(5) "admin"
["fullname"]=>
string(13) "administrator"
["phone"]=>
string(11) "15887288437"
["password_reset_token"]=>
string(32) "98641e30ace18a76f07bd7e5fbf7cd6c"
["access_token"]=>
string(32) "43641e30ace18a74207b27e3f3f3d6ff"
["email"]=>
string(12) "admin@qq.com"
["password"]=>
string(32) "98641e30ace18a76f07bd7e5fbf7cd6c"
["login_times"]=>
int(5)
["login_ip"]=>
string(9) "127.0.0.1"
["login_time"]=>
int(1608477052)
["last_login_ip"]=>
string(9) "127.0.0.1"
["last_login_time"]=>
int(1608477052)
["user_agent"]=>
string(0) ""
["create_time"]=>
int(1608477052)
["update_time"]=>
int(1608477052)
["status"]=>
int(1)
}
}
最后特别提醒:请在测试过程中删除缓存目录;
转载注明:
感谢博主,喝杯咖啡~
感谢博主,喝杯咖啡~
还没有人发表评论