Home > PHP Framework > ThinkPHP > body text

How thinkphp can delete data regularly every day

PHPz
Release: 2023-04-11 14:13:28
Original
480 people have browsed it

In actual application development, we often need to regularly delete some expired or useless data in order to maintain data cleanliness and database performance. In the ThinkPHP framework, we can easily implement the function of regularly deleting data every day through the Timer class.

The following is the implementation idea:

  1. First, you need to encapsulate the code of the scheduled task into a method, such as deleteExpiredData().
  2. In the ThinkPHP entry file index.php, instantiate a Timer object, register a scheduled task through it, and specify the deleteExpiredData() method to be executed regularly every day.
  3. In the deleteExpiredData() method, write code to delete expired data, for example, query all data that was created earlier than yesterday and delete it.

The specific implementation steps are as follows:

  1. Create a Test module and create a controller named Task in the module directory. The code is as follows:
namespace app\test\controller;

use think\Controller;
use think\Db;
use think\facade\Log;

class Task extends Controller
{
    public function deleteExpiredData()
    {
        $yesterday = date('Y-m-d', strtotime('-1 day')); //获取昨天的日期
        $where = ['create_time' => ['<&#39;, $yesterday]]; //查询条件
        $res = Db::name(&#39;test&#39;)->where($where)->delete(); //执行数据删除操作
        Log::write('删除了'.$res.'条过期数据'); //记录日志
    }
}
Copy after login

Taking the Test module as an example, query the data in the test table under the Test module that was created earlier than yesterday and delete it, and record the number of deleted data in the log.

  1. In the ThinkPHP entry file index.php, instantiate the Timer object and register the scheduled task. The code is as follows:
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Log;
use think\console\Schedule;

require __DIR__ . '/../thinkphp/base.php'; //载入ThinkPHP框架

//定时任务注册
$schedule = new Schedule();
$schedule->call('app\test\controller\Task@deleteExpiredData') //每天执行deleteExpiredData()方法
    ->daily()
    ->at('00:00'); //指定执行时间

//Timer对象实例化
$timer = new \think\Timer();
$timer->add(86400, function () use ($schedule) {
    $schedule->run(); //执行定时任务
});

$timer->start(); //启动定时器
Copy after login

Here, a Schedule object is instantiated first , used to manage scheduled tasks. Then specify the task to be executed every day through the daily() method, and specify the task execution time through the at() method, which is 00:00 every day. Then register the scheduled task through the add() method of the Timer object, and specify the execution interval of the task as one day (ie 86400 seconds). Finally, start the timer and wait for the task to be executed.

  1. Put the program on the server and run it to achieve the function of regularly deleting expired data every day.

Summary:

This article introduces specific implementation ideas and steps for the need to delete data regularly every day under the ThinkPHP framework. Among them, the Timer class and Schedule class are mainly used. Through the methods of these classes, the function of executing specified tasks regularly every day is realized, which greatly reduces the development difficulty and workload.

The above is the detailed content of How thinkphp can delete data regularly every day. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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