6-12ThinkPHP5模型的查询操作:find()/select()/get()/all()操作

6-12ThinkPHP5模型的查询操作:find()/select()/get()/all()操作

宋丰堂乡察脸憾护洼补仟痢疾

6-12ThinkPHP5模型的查询操作:find()/select()/get()/all()操作第1张

6-12ThinkPHP5模型的查询操作:find()/select()/get()/all()操作第2张

数据表结构:

6-12ThinkPHP5模型的查询操作:find()/select()/get()/all()操作第3张

使用模型实例化调用find(),get()方法

查询id=2的黄蓉

D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:

<?php
namespace app\index\controller;
use app\index\model\Staff;
class Index
{
    public function index()
    {
        //使用模型实例化的方式调用find(),get()
        $staff=new Staff();
        $where=function ($query)
        {
            $query->field(['name','salary'])
                ->where('id','=',2);
        };
        $result=$staff->find($where);
        dump($result);
    }
}

执行:

6-12ThinkPHP5模型的查询操作:find()/select()/get()/all()操作第4张

查看原始信息:

<?php
namespace app\index\controller;
use app\index\model\Staff;
class Index
{
    public function index()
    {
        //使用模型实例化的方式调用find(),get()
        $staff=new Staff();
        $where=function ($query)
        {
            $query->field(['name','salary'])
                ->where('id','=',2);
        };
        $result=$staff->find($where);
        dump($result->getData());
    }
}

执行:

6-12ThinkPHP5模型的查询操作:find()/select()/get()/all()操作第5张

给字段取别名

<?php
namespace app\index\controller;
use app\index\model\Staff;
class Index
{
    public function index()
    {
        //使用模型实例化的方式调用find(),get()
        $staff=new Staff();
        $where=function ($query)
        {
            $query->field(['name'=>'姓名','salary'=>'工资'])
                ->where('id','=',2);
        };
        $result=$staff->find($where);
        dump($result->getData());
    }
}

执行:

6-12ThinkPHP5模型的查询操作:find()/select()/get()/all()操作第6张

find()只会查询到符合条件的第一条数据

<?php
namespace app\index\controller;
use app\index\model\Staff;
class Index
{
    public function index()
    {
        //使用模型实例化的方式调用find(),get()
        $staff=new Staff();
        $where=function ($query)
        {
            $query->field(['name'=>'姓名','salary'=>'工资'])
                ->where("id>2");
        };
        $result=$staff->find($where);
        dump($result->getData());
    }
}

执行:

6-12ThinkPHP5模型的查询操作:find()/select()/get()/all()操作第7张


get()方法返回满足条件的第一条数据

<?php
namespace app\index\controller;
use app\index\model\Staff;
class Index
{
    public function index()
    {
        //使用模型实例化的方式调用find(),get()
        $staff=new Staff();
        $where=function ($query)
        {
            $query->field(['name'=>'姓名','salary'=>'工资'])
                ->where("id>2");
        };
        $result=$staff->get($where);
        dump($result->getData());
    }
}

执行:

6-12ThinkPHP5模型的查询操作:find()/select()/get()/all()操作第7张


select()方法

<?php
namespace app\index\controller;
use app\index\model\Staff;
class Index
{
    public function index()
    {
        //使用模型实例化的方式调用select(),all()
        $staff=new Staff();
        $where=function ($query)
        {
            $query->field(['name'=>'姓名','salary'=>'工资'])
                ->where("id>2");
        };
        $result=$staff->select($where);
        dump($result);//select()不能使用getData()因为返回的是结果集,会报错
    }
}

执行:

6-12ThinkPHP5模型的查询操作:find()/select()/get()/all()操作第9张

查看工资>8000的员工信息

<?php
namespace app\index\controller;
use app\index\model\Staff;
class Index
{
    public function index()
    {
        //使用模型实例化的方式调用select(),all()
        $staff=new Staff();
        $where=function ($query)
        {
            $query->field(['name'=>'姓名','salary'=>'工资'])
                ->where("salary>8000");
        };
        $result=$staff->select($where);
        //使用foreach遍历数据
        foreach ($result as $value)
        {
            dump($value);
        }
    }
}

执行:

6-12ThinkPHP5模型的查询操作:find()/select()/get()/all()操作第10张

查看原始数据

<?php
namespace app\index\controller;
use app\index\model\Staff;
class Index
{
    public function index()
    {
        //使用模型实例化的方式调用select(),all()
        $staff=new Staff();
        $where=function ($query)
        {
            $query->field(['name'=>'姓名','salary'=>'工资'])
                ->where("salary>8000");
        };
        $result=$staff->select($where);
        //使用foreach遍历数据
        foreach ($result as $value)
        {
            dump($value->getData());
        }
    }
}

执行:

6-12ThinkPHP5模型的查询操作:find()/select()/get()/all()操作第11张

all()方法:

<?php
namespace app\index\controller;
use app\index\model\Staff;
class Index
{
    public function index()
    {
        //使用模型实例化的方式调用select(),all()
        $staff=new Staff();
        $where=function ($query)
        {
            $query->field(['name'=>'姓名','salary'=>'工资'])
                ->where("salary>8000");
        };
        $result=$staff->all($where);
        //使用foreach遍历数据
        foreach ($result as $value)
        {
            dump($value->getData());
        }
    }
}

执行:

6-12ThinkPHP5模型的查询操作:find()/select()/get()/all()操作第11张

查看工资>6000

<?php
namespace app\index\controller;
use app\index\model\Staff;
class Index
{
    public function index()
    {
        //使用模型实例化的方式调用select(),all()
        $staff=new Staff();
        $where=function ($query)
        {
            $query->field(['name'=>'姓名','salary'=>'工资'])
                ->where("salary>6000");
        };
        $result=$staff->all($where);
        //使用foreach遍历数据
        foreach ($result as $value)
        {
            dump($value->getData());
        }
    }
}

执行:

6-12ThinkPHP5模型的查询操作:find()/select()/get()/all()操作第13张

格式化输入数据

<?php
namespace app\index\controller;
use app\index\model\Staff;
class Index
{
    public function index()
    {
        //使用模型实例化的方式调用select(),all()
        $staff=new Staff();
        $where=function ($query)
        {
            $query->field(['name','salary'])
                ->where("salary>6000");
        };
        $result=$staff->all($where);
        //使用foreach遍历数据
        foreach ($result as $key=>$value)
        {
            echo "第".($key+1)."条记录:姓名是".$value->name.",工资是".$value->salary."<br>";
        }
    }
}

执行:

6-12ThinkPHP5模型的查询操作:find()/select()/get()/all()操作第14张


使用模型类来静态调用find()和get()方法

查询id=7的老顽童的信息

<?php
namespace app\index\controller;
use app\index\model\Staff;
class Index
{
    public function index()
    {
        //用模型类来静态调用find()和get()方法
        $where=function ($query)
        {
            $query->field(['name','salary','age'])
                ->where('id=7');
        };
        $result=Staff::find($where);
        dump($result->getData());//单条数据可以直接使用getData()
    }
}

执行:

6-12ThinkPHP5模型的查询操作:find()/select()/get()/all()操作第15张

get()方法:

<?php
namespace app\index\controller;
use app\index\model\Staff;
class Index
{
    public function index()
    {
        //用模型类来静态调用find()和get()方法
        $where=function ($query)
        {
            $query->field(['name','salary','age'])
                ->where('id=7');
        };
        $result=Staff::get($where);
        dump($result->getData());//单条数据可以直接使用getData()
    }
}

执行:

6-12ThinkPHP5模型的查询操作:find()/select()/get()/all()操作第16张


用模型类来静态调用select()和all()方法

<?php
namespace app\index\controller;
use app\index\model\Staff;
class Index
{
    public function index()
    {
        //用模型类来静态调用select()和all()方法
        $where=function ($query)
        {
            $query->field(['name','salary','age'])
                ->where('id','>','7');
        };
        $result=Staff::select($where);
        dump($result);
    }
}

执行:

6-12ThinkPHP5模型的查询操作:find()/select()/get()/all()操作第17张

遍历输出:

<?php
namespace app\index\controller;
use app\index\model\Staff;
class Index
{
    public function index()
    {
        //用模型类来静态调用select()和all()方法
        $where=function ($query)
        {
            $query->field(['name','salary','age'])
                ->where('id','>','7');
        };
        $result=Staff::select($where);
        foreach ($result as $value)
        {
            dump($value);
        }
    }
}

执行:

6-12ThinkPHP5模型的查询操作:find()/select()/get()/all()操作第18张

查看原始数据

<?php
namespace app\index\controller;
use app\index\model\Staff;
class Index
{
    public function index()
    {
        //用模型类来静态调用select()和all()方法
        $where=function ($query)
        {
            $query->field(['name','salary','age'])
                ->where('id','>','7');
        };
        $result=Staff::select($where);
        foreach ($result as $value)
        {
            dump($value->getData());
        }
    }
}

执行:

6-12ThinkPHP5模型的查询操作:find()/select()/get()/all()操作第19张

all()方法:

<?php
namespace app\index\controller;
use app\index\model\Staff;
class Index
{
    public function index()
    {
        //用模型类来静态调用select()和all()方法
        $where=function ($query)
        {
            $query->field(['name','salary','age'])
                ->where('id','>','7');
        };
        $result=Staff::all($where);
        foreach ($result as $value)
        {
            dump($value->getData());
        }
    }
}

执行:

6-12ThinkPHP5模型的查询操作:find()/select()/get()/all()操作第20张


6-12ThinkPHP5模型的查询操作:find()/select()/get()/all()操作第21张


扭密臼蟹显念戎你烹楔镁村儒