【问题标题】:How to Insert data automatically in Yii2?Yii2如何自动插入数据?
【发布时间】:2015-05-04 14:51:18
【问题描述】:

这里我需要在满足条件时使用 Yii2 自动在数据库中插入数据记录

我有两张桌子claimpending

在索赔表中,我有 10 个字段,其中我还有另一个字段名为 turnaroundtime,我将在这里设置一个日期

如果声明表在turnaroundtime 或之前没有更新,则声明表中的一些数据必须移动到另一个名为pending 的表中。

如何使用 Yii 2 框架实现这一目标

注意:在索赔表中,我也有 created_atupdated_at 字段

【问题讨论】:

    标签: php yii2


    【解决方案1】:

    您应该设置一个 cronjob 以在特定时间间隔运行以调用一些 php 代码,该代码将检查当前时间、周转时间和更新时间之间的差异,并在满足条件时将数据移动到挂起表

    if((current-time >= turnaroundtime) && (updated_at > turnaroundtime)){
        //move data to pending table
    }
    

    【讨论】:

    • 非常感谢它的工作,我如何每次都运行 cmd 提示符
    • 您需要设置 cron 作业的工作间隔时间。如果是unix服务器,运行crontab -e设置cron作业。
    • docs.phplist.com/CronJobExamples.htmlthesitewizard.com/general/set-cron-job.shtml。 cronjobs 由网络服务器提供。如果在 cpanel 中启用了 cronjobs,您可以在 Advanced 选项卡下看到它。如果您喜欢这个答案,请不要忘记为我的答案投票!
    【解决方案2】:

    在 Yii2 yii\db\BaseActiveRecord 你可以使用这个beforeSave 事件。

    此方法在插入或更新一个开始时调用 记录。

    public function beforeSave($insert)
    {
        if($insert) { 
            // If data is new
            $this->created_at = date("Y-m-d H:i:s");
        } else { 
            //It's updating
            $this->updated_at = date("Y-m-d H:i:s"); 
        }
    
        //Make sure you return true because it's an event
        return true;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      相关资源
      最近更新 更多