Scheduled Task on CakePHP

孤者浪人 提交于 2019-12-10 19:35:14

问题


I work on CakePHP 3.2 Project..

I have a Property Entity..

when a user creates a property, the admin must validate it to become active.. After that I put in field called date_of_expiration the current date + 10 days for example ...

What I want is that this property expires in this date (current date + 10 day).. By changing a field called status from active to inactive..

I searched in Google and i found that what i nead called Sheduled Task..

I ask about the best way to do this in CakePHP 3.2


回答1:


  1. Create Shell with the function to find an expired property and change the value to inactive.
  2. Run Shell from CronJob every day at 00:00:00



回答2:


You could set up a cron job which would call a function in your controller. Your function then would select all the records from your properties table, check if date_of_expiration is expired and then set the status to inactive.

You have to allow the method to be called without you are logged in and possibly disable the CSRF component (if you are using it):

public function beforeFilter(Event $event){
    $this->Auth->allow('cronjob_expiration_date');

    if(in_array($this->request->action, ['cronjob_expiration_date'])) {
        $this->eventManager()->off($this->Csrf);
    }
}

This is necessary, because the cron job "user" is not logged in.



来源:https://stackoverflow.com/questions/36768676/scheduled-task-on-cakephp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!