Home > PHP Framework > ThinkPHP > Model operations in thinkphp

Model operations in thinkphp

Release: 2020-05-09 09:12:04
forward
2730 people have browsed it

Model operations in thinkphp

Create a new model

There are two methods:

One is to create it manually

1. Create a new model folder

2. Create a new file user.php. It is best that the name corresponds to the table name

3. Write the code

<?php
namespace app\index\model;
use think\Model;
class User extends Model
{
//如果表名和文件名不是对应的,用下面代码修改
protected $table = &#39;think_user&#39;;
    }
Copy after login

The second is to use the command:

>php think make:model index/Blog
Copy after login

Model instantiation

There are three ways:

1. Use static method

use app\index\model\User;
 $li= User::get(1);
Copy after login

2. Use loading class

use think\Loader;
$user= Loader::model(&#39;user&#39;);
 $li= $user::get(1);
Copy after login

3. Use system method

$user= model(&#39;User&#39;);
 $li= $user::get(1);
Copy after login

Query operation of database

get means to query the data whose primary key is 1. The following is to query the data of name=thinkphp

//取出主键为1的数据
$user = User::get(1);
// 使用数组查询
$user = User::get([&#39;name&#39; => &#39;thinkphp&#39;]);
Copy after login

You can also use the system query method

$user = new User();
$user->where(&#39;name&#39;, &#39;thinkphp&#39;)->find();
Copy after login

To query multiple pieces of data

Use the all() method

// 根据主键获取多个数据
$list = User::all(&#39;1,2,3&#39;);
// 或者使用数组
$list = User::all([1,2,3]);
foreach($list as $key=>$user){
    echo $user->name;
}
// 使用数组查询
$list = User::all([&#39;status&#39;=>1]);
// 使用闭包查询
$list = User::all(function($query){
    $query->where(&#39;status&#39;, 1)->limit(3)->order(&#39;id&#39;, &#39;asc&#39;);
});
foreach($list as $key=>$user){
    echo $user->name;
}
Copy after login

Or use the query method to view

$user = new User();
// 查询数据集
$user->where(&#39;name&#39;, &#39;thinkphp&#39;)
    ->limit(10)
    ->order(&#39;id&#39;, &#39;desc&#39;)
    ->select();
Copy after login

Query a certain field value

// 获取某个用户的积分
User::where(&#39;id&#39;,10)->value(&#39;score&#39;);
// 获取某个列的所有值
User::where(&#39;status&#39;,1)->column(&#39;name&#39;);
// 以id为索引
User::where(&#39;status&#39;,1)->column(&#39;name&#39;,&#39;id&#39;);
User::where(&#39;status&#39;,1)->column(&#39;id,name&#39;);
Copy after login

You can also use dynamic query

// 根据name字段查询用户
$user = User::getByName(&#39;thinkphp&#39;);
// 根据email字段查询用户
$user = User::getByEmail(&#39;thinkphp@qq.com&#39;);
Copy after login

New data

Use save method

$user           = new User;
$user->name     = &#39;thinkphp&#39;;
$user->email    = &#39;thinkphp@qq.com&#39;;
$user->save();
Copy after login

Use data batch method to save

$user = new User;
$user->data([
    &#39;name&#39;  =>  &#39;thinkphp&#39;,
    &#39;email&#39; =>  &#39;thinkphp@qq.com&#39;
]);
$user->save();
Copy after login

The most commonly used is to import post or get data during instantiation, and then save it##I am I feel it is more important here

//如果需要过滤非数据表字段的数据,可以使用:
$user = new User($_POST);
// 过滤post数组中的非数据表字段数据
$user->allowField(true)->save();

//如果你通过外部提交赋值给模型,并且希望指定某些字段写入,可以使用:
$user = new User($_POST);
// post数组中只有name和email字段会写入
$user->allowField([&#39;name&#39;,&#39;email&#39;])->save();
Copy after login

Get the inserted data id

$user           = new User;
$user->name     = &#39;thinkphp&#39;;
$user->email    = &#39;thinkphp@qq.com&#39;;
$user->save();
// 获取自增ID
echo $user->user_id;
Copy after login

Insert multiple pieces of data

$user = new User;
$list = [
    [&#39;name&#39;=>&#39;thinkphp&#39;,&#39;email&#39;=>&#39;thinkphp@qq.com&#39;],
    [&#39;name&#39;=>&#39;onethink&#39;,&#39;email&#39;=>&#39;onethink@qq.com&#39;]
];
$user->saveAll($list);
Copy after login

You can also use the assistant function

// 使用model助手函数实例化User模型
$user = model(&#39;User&#39;);
// 模型对象赋值
$user->data([
    &#39;name&#39;  =>  &#39;thinkphp&#39;,
    &#39;email&#39; =>  &#39;thinkphp@qq.com&#39;
]);
$user->save();

或者进行批量新增:
$user = model(&#39;User&#39;);
// 批量新增
$list = [
    [&#39;name&#39;=>&#39;thinkphp&#39;,&#39;email&#39;=>&#39;thinkphp@qq.com&#39;],
    [&#39;name&#39;=>&#39;onethink&#39;,&#39;email&#39;=>&#39;onethink@qq.com&#39;]
];
$user->saveAll($list);
Copy after login

Data deletion , need to query first, then delete

Delete one item

$user = User::get(1);
$user->delete();
Copy after login

Delete multiple items

User::destroy(1);
// 支持批量删除多个数据
User::destroy(&#39;1,2,3&#39;);
// 或者
User::destroy([1,2,3]);
Copy after login

Delete according to conditions

// 删除状态为0的数据
User::destroy([&#39;status&#39; => 0]);

还支持使用闭包删除,例如:
User::destroy(function($query){
    $query->where(&#39;id&#39;,&#39;>&#39;,10);
});

或者通过数据库类的查询条件删除
User::where(&#39;id&#39;,&#39;>&#39;,10)->delete();
Copy after login

Data update , need to query first, and then update

$user = User::get(1);
$user->name     = &#39;thinkphp&#39;;
$user->email    = &#39;thinkphp@qq.com&#39;;
$user->save();
Copy after login

Update according to conditions

$user = new User;
// save方法第二个参数为更新条件
$user->save([
    &#39;name&#39;  => &#39;thinkphp&#39;,
    &#39;email&#39; => &#39;thinkphp@qq.com&#39;
],[&#39;id&#39; => 1]);
Copy after login

Post submission direct update

$user = new User();
// 过滤post数组中的非数据表字段数据
$user->allowField(true)->save($_POST,[&#39;id&#39; => 1]);

//如果你通过外部提交赋值给模型,并且希望指定某些字段写入,可以使用:
$user = new User();
// post数组中只有name和email字段会写入
$user->allowField([&#39;name&#39;,&#39;email&#39;])->save($_POST, [&#39;id&#39; => 1]);
Copy after login

Forced update to prevent adding

$user = new User;
$list = [
    [&#39;id&#39;=>1, &#39;name&#39;=>&#39;thinkphp&#39;, &#39;email&#39;=>&#39;thinkphp@qq.com&#39;],
    [&#39;id&#39;=>2, &#39;name&#39;=>&#39;onethink&#39;, &#39;email&#39;=>&#39;onethink@qq.com&#39;]
];
$user->isUpdate()->saveAll($list);
Copy after login

Use database class to add

$user = new User;
$user->where(&#39;id&#39;, 1)
    ->update([&#39;name&#39; => &#39;thinkphp&#39;]);
或者使用:
$user = new User;
$user->update([&#39;id&#39; => 1, &#39;name&#39; => &#39;thinkphp&#39;]);
Copy after login

Recommended tutorial: "TP5"

The above is the detailed content of Model operations in thinkphp. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template