search

Home  >  Q&A  >  body text

php - How to make sure the time set by crontab to execute tasks regularly?

Take the yii2 framework as an example:
The timing configuration file console\config\params.php is configured as follows:

< /p>

Question:

1. Get the timing of the red box in the above picture from the database. When encountering
`PHP Fatal error: Call to a member function getDb() on null in /home/wwwroot/default/ceshi/vendor/yiisoft/yii2/db/ActiveRecord.php on line 133`
Error, how to solve it?

2. How to obtain database data in params.php of yii2? 
PHPzPHPz2774 days ago569

reply all(2)I'll reply

  • 習慣沉默

    習慣沉默2017-05-16 13:02:57

    Write it in the form of an anonymous function

    return [
        'adminEmail'=>'admin@xx.com',
        'cronJobs'=>[
            'contact/send'=>[
                    //code
            ],
            'contact/hsend'=>[
                'cron'=>function(){
                    //todo 获取数据库中的定时配置(可以在外面写个自定义函数获取)
                    $_cron2 = you_function();
                    return $_cron2;
                }
            ]
        ]
    ]
    
    
    
    <?php
    
    $data = [
        'cronJobs'=>[
            'send'=>[
                //code
            ],
            'hsend'=>[
                'cron'=>function(){
                    return '888888';
                }
            ]
        ]
    ];
    echo $data['cronJobs']['hsend']['cron'](); //这么写

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-16 13:02:57

    (Thank you for the invitation) Sorry, I don’t know much about yii2. But for the TP approach, I will dynamically generate the time configuration, read the database setting time in the public controller, and generate the configuration parameters. In this way, you can use it happily (you can refer to it, I hope it will be helpful to you).

    reply
    0
  • Cancelreply