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.