kh.tab
kh.tab

Reputation: 1304

Scheduled Task on CakePHP

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

Upvotes: 3

Views: 3508

Answers (2)

Salines
Salines

Reputation: 5767

  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

Upvotes: 5

ST2OD
ST2OD

Reputation: 725

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.

Upvotes: -1

Related Questions