thinkphp6.0配置参数resultset_type

ThinkPHP6.* / 913人浏览 / 0人评论

thinkphp6取消resultset_type配置参数,数据集查询结果不再受resultset_type配置参数影响,默认情况下,Db查询统一返回数组,模型查询统一返回模型对象和模型数据集对象。如果Db查询的时候也需要返回数据集的话,可以显式调用fetchCollection方法。

thinkphp6取消resultset_type配置参数

版本:const VERSION = '6.0.8';

数据集查询结果不再受resultset_type配置参数影响,默认情况下,Db查询统一返回数组,模型查询统一返回模型对象和模型数据集对象。如果Db查询的时候也需要返回数据集的话,可以显式调用fetchCollection方法。

1、首先下载安装thinkphp6.0,浏览器打开运行。

参考文档:https://www.kancloud.cn/manual/thinkphp6_0/1037481

thinkphp6.0

2、配置数据库链接、本地随便选择电脑中已经有的库来用。

就在Index控制器index方法下使用Thinkphp6Db类操作数据库。

a)、查找一条记录:find()。

<?php
namespace app\controller;

use app\BaseController;
use think\facade\Db;
class Index extends BaseController
{
    public function index()
    {
        $result = Db::table('onethink_document')->where('id', 1)->select();
        var_dump($result);
        die;        
        //return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">14载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
    }

    public function hello($name = 'ThinkPHP6')
    {
        return 'hello,' . $name;
    }
}

返回如下,是数组形式。

array(1) {
  ["title"]=>
  string(26) "OneThink1.1开发版发布"
}

b)、查找一条记录:select()。

<?php
namespace app\controller;

use app\BaseController;
use think\facade\Db;
class Index extends BaseController
{
    public function index()
    {
        $result = Db::table('onethink_document')->field('title')->where('id', 1)->select();
        var_dump($result);
        die;        
        //return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">14载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
    }

    public function hello($name = 'ThinkPHP6')
    {
        return 'hello,' . $name;
    }
}

返回如下,是对象数组形式。

object(think\Collection)#54 (1) {
  ["items":protected]=>
  array(1) {
    [0]=>
    array(1) {
      ["title"]=>
      string(26) "OneThink1.1开发版发布"
    }
  }
}

正确使用,看下面,对象转数组,特别说明Db::select()后不能直接使用,toArray()

<?php
namespace app\controller;

use app\BaseController;
use think\facade\Db;
class Index extends BaseController
{
    public function index()
    {
        $result = Db::table('onethink_document')->field('title')->where('id', 1)->select();
        if($result->isEmpty() == false) {
            $result = $result->toArray();
            var_dump($result);
        }
        die;
        //return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">14载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
    }

    public function hello($name = 'ThinkPHP6')
    {
        return 'hello,' . $name;
    }
}

返回结果

array(1) {
  [0]=>
  array(1) {
    ["title"]=>
    string(26) "OneThink1.1开发版发布"
  }
}

做个小结

如果是select查询返回的就是对象数组,如果要判断是否为空,请使用:$result->isEmpty() == false 表示不空,在使用$result->toArray()转成数组。
如果未查询到数据直接使用就会报错:比如这样使用(Db::table('onethink_document')->field('title')->where('id', 1)->select()->toArray())。

会提示错误:Call to a member function toArray() on null

转载注明:

扩展查找

0 条评论

还没有人发表评论

发表评论 取消回复

记住我的信息,方便下次评论
有人回复时邮件通知我