如何在 codeigniter 的不同时间戳为 table 中的每个用户执行更新?

How to perform update for each users in table at different time stamp in codeigniter?

对于 Codeigniter 中的 CRM 系统,我有一个管理模块,可以将余额添加到转销商。现在经销商可以选择查看他自己的用户,他可以添加以删除余额。现在,如果在编辑时有一个单选按钮,余额是 Yes 或 No.Here 我知道逻辑但不知道如何实现以下内容:一个经销商编辑用户并将余额设置为是 我想计算30 天,30 天后余额将重置为无。所以我认为我应该获取所有用户并检查他们的余额何时设置为是。如果已经 30 天了,那么我有一个更新 cron 作业文件,它将余额重置为否。现在我不知道该怎么做?我怎么能检查每个用户?我对代码和逻辑很困惑。下面是我编辑用户的代码。

public function edit ($id = NULL)
{

     $usertype=$this->session->userdata('usertype');
    if($usertype ==="reseller")
    {

    // Fetch a user or set a new one
    if ($id) {
        $this->data['user'] = $this->user_m->get($id);
        count($this->data['user']) || $this->data['errors'][] = 'User could not be found';
    }
    else {
        $this->data['user'] = $this->user_m->get_new();
    }

    // Set up the form
    $rules = $this->user_m->rules_admin;
    $id || $rules['password']['rules'] .= '|required';
    $this->form_validation->set_rules($rules);

    // Process the form
    if ($this->form_validation->run() == TRUE) {
        $data = $this->user_m->array_from_post(array('sip_id','sip_pass','name','email', 'password','phone','status','created','balance'));
        $data['password'] = $this->user_m->hash($data['password']);
        $this->user_m->save($data, $id);
        redirect('reseller/user');
    }

    // Load the view
    $this->data['subview'] = 'reseller/user/edit';
    $this->load->view('reseller/_layout_main', $this->data);


}
else{
    $this->load->view('permission');
}

}

经销商:

用户Table:

您的数据库 (MySQL) 的触发器示例:

CREATE TRIGGER resetBalance
    BEFORE UPDATE ON your_table_name
    IF new.balance = "yes" THEN 
    SET NEW.reset = ADDDATE(curdate(), INTERVAL 30 DAY);
    End IF

更新:

比方说,您的数据库中有一个名为 balance 的字段,您可以在其中存储值 yes 或 no。 (我更喜欢布尔类型。)

假设您有一个名为重置的字段,您可以在其中保存余额的重置日期。重置字段应该是日期时间。

如果您对任何余额字段进行更新并且余额字段的 «new» 条目为是,数据库将在重置字段中插入当前日期 + 30 天。

New 表示你在数据库中保存的新条目,也有一个旧值。这是列中更新之前的值。

就这些了。

在您的 cron 中,您只需检查重置字段,执行一些逻辑或其他操作,然后删除重置字段中的值。

更新 2:

我刚看到,您的数据库中有一个创建和修改的字段。这里也是使用触发器的最佳方式。

插入前:

set new.created = NOW();

更新前:

set new.modified = NOW();

更新 3:

今天是触发日 ;-))

假设您有一个 tblUsers 和一个 tblResellers。在 tblResellers 中,您希望根据 tblUsers 中的余额字段设置重置日期。然后你应该像这样在 tblResellers 中创建一个触发器:

BEGIN
set @userBalance = (SELECT balance FROM tblUsers WHERE id = old.userid);

IF @userBalance = "yes" THEN 
    SET NEW.reset = ADDDATE(curdate(), INTERVAL 30 DAY);
    ELSE
    SET NEW.reset = "";
    END IF;
END

在第二行中,我定义了一个变量 «userBalance»,它从 tblUsers 获取值。在 where 子句中,我使用了一个来自 tblResellers (userid) 的字段,它对应于 tblUsers 中的一个 ID。

上面几行很简单:检查 userBalance 的值,做出一些决定并设置重置日期。

我认为此触发器无法满足您的所有需求,但您对如何操作有所了解。如果改了代码,一步一步来,调试触发器有点棘手。