如何编写触发器在30天后重置字段的值? [英] How Do I Write a Trigger That resets the value of a field after 30 days?

查看:81
本文介绍了如何编写触发器在30天后重置字段的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户表,我有一个平衡字段!经销商可以编辑用户表单,并可以将余额设置为是或否。如果他将余额设置为是,那么30天后,我想将该用户的余额重置为否。每个用户由一个称为key的唯一字段映射。所以一旦余额设置为是触发器应该为该特定用户计数30天,并在30天后将其重置为否。

I have a user table where i have a balance field! A Reseller can edit a user thorugh a form and may set balance to Yes Or No. If He Sets the balance to yes then after 30 days i want to reset the balance of only that user to no. Each user is mapped by a unique field called key. So once the balance is set to yes trigger should count 30 days for that specific user and reset it back to No after 30 days

我使用PHP Codeigniter。 >

i am using PHP Codeigniter.

推荐答案

关于设置cron作业请参考此页面: http://www.thesitewizard.com/general/set-cron-job.shtml 在此输入链接描述

About setting a cron job refer to this page: http://www.thesitewizard.com/general/set-cron-job.shtmlenter link description here

除此之外, do:
首先应该为cron作业创建一个控制器类。这实际上是您网站上的一个网页,访问时会执行您想要的代码。

Apart form that, about the thing you want to do: First you should create a controller class for cron jobs. This is actually a page on your website, that, when accessed, executes the code you want.

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class cronjobs extends CI_Controller {   
        public function __Construct(){  

        }   
        public function index(){echo 'Nothing to see here.'}
        public function reset_resellers(){
            $this->load->model('your_model');
            echo $this->your_model->reset_resellers() . "resellers were reset.";
        }
}

因此,如果您的cron作业设置为访问此页面每30天一次,你得运行脚本一样多次。
关于实际的脚本,这是很难回答。但是我们会尝试:
你必须有一个更新数据库的模型,或多或少这样:

So if your cron job is set to access this page once every 30 days, you get to run the script just as many times. About the actual script, this is harder to answer. But we'll try: You must have a model that updates the database, more or less like this:

update reseller.balance = 'no' where balance = 'yes' and balance_set_date = today - 30 days

因此,您必须在代理商表中保留 balance_set_date 的列,但是您可以用不同的方式调用它,保持 date 格式,而不是 timestamp 格式,让我们尝试创建该模型:

So you must have a column in your resellers table where you keep balance_set_date but you might call it differently, and suppose you keep that in date format, and not in timestamp format, let's try to create that model:

function reset_resellers(){
   $this->db->where('balance', 'yes');
   $this->db->where('balance_set_date <= CURDATE() - 30', '', false);
   $query = $this->db->update('resellers');
   return $query->num_rows();
}

最后,如果你想检查你的工作是否会做你想要的,只需访问网址 www.example.com/cronjobs/reset_resellers

And finally, if you want to check if your job would do what you want, just access the url www.example.com/cronjobs/reset_resellers

这篇关于如何编写触发器在30天后重置字段的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆