Scheduled Task on CakePHP

2019-07-14 21:24发布

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

2条回答
三岁会撩人
2楼-- · 2019-07-14 22:03
  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
查看更多
Deceive 欺骗
3楼-- · 2019-07-14 22:05

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.

查看更多
登录 后发表回答