Blogger Information
Blog 61
fans 0
comment 0
visits 62893
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
tp5.1 模型与数据库表绑定
Pengsir
Original
1061 people have browsed it
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/22 0022
 * Time: 16:55
 */

namespace app\index\controller;
use app\index\model\Student;
//use think\Db;

/*
 * 模型是和一张数据表绑定的 这张表写在自定义模块model下,并且这个表必须与数据库的表同名
 * 好处:1.模型和一张用户自定义表绑定,我们进行数据库操作的时候,我们就不用再去选择数据表了
 *      2.返回的始终是个对象
 * 前面讲的用数据库访问返回的是数组
 */
class Demo6
{
    public function get(){
//        dump(Student::get(3));//这个查询比较复杂的查不了
        //用查询构造器创建更加复杂的查询
        $res=Student::field('id,name,email')
            ->where('id',3)
            ->find();
//        $res=Db::table('student')->field('id,name,email')
//            ->where('id',3)
//            ->find();
//        相当于Db::table('student')==Student:
        dump($res);

    }
//    查询多条
    public function all(){
//        dump(Student::all([1,2,3]));
//        用查询构造器创建更加复杂的查询
        $res=Student::field('id,name,email')
            ->select();
        dump($res);
    }
}

新建模块model下的Student表代码:

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/22 0022
 * Time: 17:09
 */
namespace app\index\model;

use think\Model;

class Student extends Model
{
//    protected $table = '对应一张数据库表名';//这里可以设置绑定数据库的那张表,不写就是我们这里设的Student表
}

新建模块model下的Student表:

新建模块model下的对应数据库的Student.png

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post