The second Thinkphp5.0

Query builder

//插入记录
    $result=Db::table('think_data')->insert(['name'=>'张三','status'=>1]);
//修改数据
            $result=Db::table('think_data')->where('id', 1)->update(['name'=>"陈国松",'status'=>0]);
//查询数据
$result=Db::table('think_data')->select();

(Seemingly can not modify the data may be modified id id added AUTO_INCREMENT, would not be able to function without assistant helper)

Chaining

Chaining method provided by the database, can effectively improve the development efficiency and clarity of code data access, and supports all CURD operations.
Use is also relatively simple, if we want to query a status of the User table satisfies the first 10 rows of 1, and in accordance with the desired discharge time the user created
sequence, as follows:

Db::table('think_user')
->where('status',1)
->order('create_time')
->limit(10)
->select();

Here where, order and to limit the method is called a method of operating the chain, in addition to select a method must be placed outside the last
(because the select method is not chaining method), a method of operation of the chain has no calling sequence, e.g. code below and above the like
effect:

Db::table('think_user')
->order('create_time')
->limit(10)
->where('status',1)
->select();

In fact, not only you can use the query methods consistent operations, including all CURD methods can be used, for example:

Db::table('think_user')
->where('id',1)
->field('id,name,email')
->find();
Db::table('think_user')
->where('status',1)
->where('id',1)
->delete();

Chain automatically clears all by value chain operations after the completion of inquiry. In short, the results of operations of the chain will not inquire into the back of the other.

Query data

  $result=Db::name('data')->where('status',1)->find();

  $result=Db::name('data')->where('status',1)->select();

Difference find and select methods that can only be found in a find, and select all of the query.

Query Expression

Fuzzy query

      $result=Db::name('data')->where('name','like','%陈%')->select();

Interval inquiry

            $result=Db::name('data')->where('id','BETWEEN',[2,4])->select();

One inserts a plurality of data

            $data=[

     ['name'=>"凌志林",'status'=>1],

      ['name'=>"林志颖",'status'=>2],

            ];

            $result=Db::name('data')->insertALL($data);

Change the field value

            $result=Db::name('data')->where('name','陈国松')->setField('name','陈松');

Increase from a

$result=Db::name('data')->where('name','陈松')->setInc('status');

Decrement

$result=Db::name('data')->where('name','陈松')->setDec('status',2);

model

Thinkphp5.0 model is a relational mapping ORM package and provides simple ActiveRecord achieved. In general, each of the data tables and will be a "model corresponds."

Model class returns an object instance of the current model, the model is more advanced than the Db class data package supporting the association model, model events.

Model definition

First create a data table

Then create the following files and folders

In which the model is as follows User.php

<?php
namespace app\index\model;
use think\Model;
class User extends Model
{
    
}

controller inside User.php

<?php

namespace app\index\controller;

use app\index\model\User as UserModel;

class User{

    //新增一条数据的方法

   public function add()

    {

       echo 'hello';

        $user =new UserModel();

        $user->id=1;

        $user->name='陈国松';

        $user->email='[email protected]';

        $user->birthday=strtotime('1989-7-11');

      if( $user->save()){

          return '用户新增成功';

      }else{
          return '用户新增失败';

     }

    }

}

Such access the address bar (not get virtual host)

http://localhost/thinkphp5.0/public/index.php/index/User/add

Successful database will be as follows

Guess you like

Origin www.cnblogs.com/chenguosong/p/11483207.html