External tasks in CakePHP, automatically execute scripts

Source: Internet
Author: User
The Cake console is a new feature in CakePHPV1.2. It provides command line interfaces with the Cake framework. To create your own command line task, you must create a shell. Shell looks very similar to the controller you have created.

The Cake console is a new feature in CakePHP V1.2. It provides command line interfaces with the Cake framework. To create your own command line task, you must create a shell. Shell looks very similar to the controller you have created.

First, create the prune. php file in the/column/protected/cakephp/app/vendors/shells directory. This is the new shell named prune, which will delete all the posts with a release date of more than 30 days. Defines a new class PruneShells, which will expand the shell class. To delete the post object, shell needs to use the post model. you can use the $ uses variable to specify the post object. By default, when Cake is told to execute shell without passing any specific operation, Cake will find a method named main, and if it finds it, it will execute this method. In this case, the empty shell looks like listing 1.

 

Now, you only need to add the code to the main method to delete all posts that have been released for more than 30 days.

$conditions = array (    "Post.modified" => "< " . date("Y-m-d H:i:s", strtotime("-30 days")));$this->Post->deleteAll($conditions);

To execute this script through the command line, you need to tell Cake to run prune shell. Because all code is in the main method, it will be executed by default. You should also tell Cake the location of the app directory. If you run commands from the app directory, you do not need to inform the app directory location, but the cron job will not run from the correct directory unless you tell it to execute the following code: /column/protected/cakephp/cake/console/cake prune-app/column/protected/cakephp/app /.

Scheduling a cron job to run at midnight to execute this shell will be similar to the following code: 00 00 ***/column/protected/cakephp/cake/console/cake prune-app/column/protected/cakephp/app /.

Note:If you add the/column/protected/cakephp/cake/console directory to the PATH, you do not need to specify the full PATH, which makes it easier to use the console. Specifying the full path in a batch or cron job will help ensure proper execution regardless of the user who executes the job.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.