


A brief analysis of the Tp5 framework for adding, deleting, modifying and checking through models
Thinkphp5 add, delete, modify and check operations (add, delete, modify and check through models)
How to add, delete, modify and check through models in Thinkphp5 is very simple.
First create a controller in the application\index\controller directory and name it: Index.php
Index.php The code is as follows, here I am in the application\index\controller module If the controller created under is not the index module, your namespace must be changed to the namespace corresponding to your own module.
<?php namespace app\index\controller; use app\index\model\Admin; class Index { public function index(){ Admin::index(); } }
Then create a model in the application\index\model directory and name it Admin.php
Admin.php The code is as follows. I created the model under the application\index\model module. , if it is not the index module, your namespace needs to be changed to the namespace corresponding to your own module.
Admin.php (Let’s talk about the model query operation first)
<?php namespace app\index\model; use think\Model; class Admin extends Model { // 设置当前模型对应的完整数据表名称 protected $table = 'user'; public static function index(){ $list = '14'; /* 单条数据查询操作,all方法是tp5框架规定的查询方法,all里面是一个闭包,作为变量传入, 不要纠结闭包构造,直接复制用即可,不需要关注$query是什么?框架内部实现时传值进去, 执行where方法,只需要关注where条件和传的变量$list即可。 $list值自己可定 如上面$list = 14,id可自己定,查询id=14这一条数据 */ $re = Admin::all(function($query) use ($list) { $query->where('id','=',$list); }); //echo '<pre/>';var_dump($re[0]->data); } }
<?php namespace app\index\model; use think\Model; class Admin extends Model { // 设置当前模型对应的完整数据表名称 protected $table = 'user'; public static function index(){ $list = ['13','14']; /* 这是多条查询,查询id=13和14这两条数据,同上述只需要关注where里面的条件即可, 如果查name或其它字段把where里的id改成name即可,$list也封装好了, 你可以传值进去,就像我上面$list 等于一个数组,*/ $re = Admin::all(function($query) use ($list) { $query->where('id','in',$list); }); // echo '<pre/>';var_dump($re); } }
<?php namespace app\index\model; use think\Model; use think\Db; class Admin extends Model { public static function index() { /* join查询,查询两个表以上的数据 Db::table 要加上use think\Db; 这里查的是user表的id等于sessions表的id,两个表里的符合条件的全部数据 */ $re = Db::table('user') ->alias('a') ->join('sessions w','a.id = w.id') ->select(); // var_dump($re);die; } }
namespace app\index\model; use think\Model; use think\Db; class Admin extends Model { // 数据表名 这里要提一下,表名必须是完整的表名,要带上表前缀,哪怕在配置文件里面设置了表前缀。 protected $table = 'fa_kindex'; // 查询 public static function index() { // 如果要查询一张表中所有字段信息,直接写个闭包在里面就可以了, $result = Index::select( function() {} ); return $result; // $offset = 0, $limit = 1, $result = Index::select( // use()里面写传进闭包中的变量,如下面的 use ($offset , $limit) function($query) use ($offset , $limit , $rid) { /* 闭包构造 $query可以衔接order,limit,where,field等条件。但不能写成 $query->select();因为这样select方法会执行两次 可以写成 $query->order('id','asc'), //根据id升序查询 $query->limit($offset , $limit) $query->field('id',true); //查询除id字段外的所有字段数据 // 查询符合id = 1的id,name字段的数据,查询结果按id升序排列 $query->field('id','name')->where('id=1')->order('id','asc'); */ $query->order('id','asc')->limit($offset , $limit)->field('id',true)->where('rid ='.$rid); } ); return $result; } }
Of course, many people say that after querying, they get class objects. That’s because tp3.2 directly returns the query results. , and tp5 does not directly return the query results. It returns the entire instantiated class and puts the query results into the data attribute of the class. So how do you get the data? Imagine how to access the attributes in the class after instantiating the class. It’s hard to understand. Use -> to get it.
echo '<pre/>';var_dump($re[0]->data);
This way you can get the data. It’s the same as tp3.2. Of course, this is a single item. If there are multiple items, the key value of $re You can use foreach to retrieve it in a loop.
Admin.php (model deletion operation)
<?php namespace app\index\model; use think\Model; class Admin extends Model { // 设置当前模型对应的完整数据表名称 protected $table = 'user'; public static function index(){ $list = '14'; /*单条数据删除操作 ,同上面的单条查询一样,只关注where条件和传的变量$list即可 $list值自己可定 如上面$list = 14,id可自己定,删除id=14这一条数据 */ Admin::destroy(function($query) use ($list) { $query->where('id','=',$list); }); } }
<?php namespace app\index\model; use think\Model; class Admin extends Model { // 设置当前模型对应的完整数据表名称 protected $table = 'user'; public static function index(){ $list = ['1','3']; /*多条数据删除操作 ,同上面的多条查询一样,只关注where条件和传的变量$list即可 $list值自己可定 如上面$list等于一个数组,id可自己定,删除id=1和id=3两条数据 */ Admin::destroy(function($query) use ($list) { $query->where('id','in',$list); }); } }
Admin.php (model modification operation)
<?php namespace app\index\model; use think\Model; class Admin extends Model { // 设置当前模型对应的完整数据表名称 protected $table = 'user'; public static function index(){ // 静态方法里执行非静态方法,new static就是本类,reindex为本类的reindex方法 $method = new \ReflectionMethod(new static,'reindex'); $method->invokeArgs(new static,array()); } public function reindex(){ $id = '5'; $list = [ 'goods_id' => $goods_id, 'user_id' => $_SESSION['user_id'], ]; /* 因为isUpdate方法是非静态方法,只能用$this调用,根据手册isUpdate(true) 是更新,isUpdate(false)是查询,tp5方法有点多还有点乱,但不要紧实际写业务逻辑时, 记住固定句型即可,像下面我们只关注save里面传入什么东西即可,跟上面的where一样, 后面写条件,前面写修改的内容,将符合$list条件的sql语句中的rid字段值修改为0; $list是条件可设置多个条件,如上面的$list数组 */ $this->isUpdate(true)->save(['rid' => '0'],$list); } }
Admin. php (model new operation)
<?php namespace app\index\model; use think\Model; class Admin extends Model { // 设置当前模型对应的完整数据表名称 protected $table = 'user'; public static function index(){ $username = 'Thinkphp'; $passwd = '123456'; /* 增加一条数据,Admin::create(),调用框架静态方法create,这里提一下Admin 是你模型的名字,如果建立的模型是user,那就写User::create(), 同样只关注create里改传入什么参数即可。新增一条数据,username插入值是$username passwd插入值是$passwd,你可以根据自己需求制定字段的值,别死板灵活点*/ Admin::create([ 'username' => $username, 'passwd' => $passwd, ]); } }
Related recommendations:The latest 10 thinkphp video tutorials
The above is the detailed content of A brief analysis of the Tp5 framework for adding, deleting, modifying and checking through models. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

RPC service based on ThinkPHP6 and Swoole implements file transfer function Introduction: With the development of the Internet, file transfer has become more and more important in our daily work. In order to improve the efficiency and security of file transfer, this article will introduce the specific implementation method of the RPC service based on ThinkPHP6 and Swoole to implement the file transfer function. We will use ThinkPHP6 as the web framework and utilize Swoole's RPC function to achieve cross-server file transfer. 1. Environmental standard
