A console controller is totally similar to the web controllers that we created earlier. It extends the yiiconsoleController
base class and can return an integer value indicating the status response of the action (0 stands for successful execution of the action), also named exit code
.
The public
properties of the controller can be made available as an option only if their names are returned by the options()
method that accepts actionID
as the parameter; so the response can be customized according to actionID
.
The response of the options()
method is an array of text string that represents the public property names of the controller.
Starting from the advanced template application that we previously installed in the yiiadv
folder, let's create a new console controller named MyExampleController
in console/controllers/MyExampleController.php
with the following content:
<?php namespace consolecontrollers; use yiiconsoleController; /** * This is an example controller */ class MyExampleController extends Controller { public $option1; public $option2; public function options($action) { return ['option1']; } /** * Simply return a welcome text */ public function actionTest($param1) { echo 'this is my first controller using console application'; echo " "; echo "You have passed param1 with value: ".$param1; echo " "; echo "Value of option1 is: ".$this->option1; echo " "; // equivalent to return 0; return Controller::EXIT_CODE_NORMAL; } } ?>
This controller contains two public properties, but only option1
will be usable from the console, since it is returned by the options()
method. We will display the result of the following command:
$ ./yii help my-example
The preceding command will return the following output:
DESCRIPTION This is an example controller SUB-COMMANDS - my-example/test Simply return a welcome text To see the detailed information about individual sub-commands, enter: yii help <sub-command>
If we need other details about the test
action, we can launch the preceding command specifying the complete route:
$ ./yii help my-example/test
Now, try to launch the command with the route my-example/test
, without any parameter:
$ ./yii my-example/test
We will receive an error about missing param1
. The following is the correct syntax:
$ ./yii my-example/test "this is value for param1"
The preceding command will return the following output without any value for option1
:
this is my first controller using console application You have passed param1 with value: this is value for param1 Value of option1 is:.
We can also pass the value option1
by appending --option1
to the command, as follows:
$ ./yii my-example/test "this is value for param1" --option1="this is value for option1"
The preceding command will return a complete output, as follows:
this is my first controller using console application You have passed param1 with value: this is value for param1 Value of option1 is: this is value for option1
Now, let's consider an example to illustrate how to use console commands to execute maintenance operations.
In console controllers, we can access all the models, components, and extensions available in the project, as well as what we have done in the web application. Therefore, we will manipulate data in the same way as we should do for a web application.
Starting from the reservation database table used in the previous chapters, we will add a new Boolean field, named expired, to set which reservations are out of the end date.
This is the structure of the reservation
table to store data in the MySQL Server:
CREATE TABLE `reservation` ( `id` int(11) NOT NULL AUTO_INCREMENT, `room_id` int(11) NOT NULL, `customer_id` int(11) NOT NULL, `price_per_day` decimal(20,2) NOT NULL, `date_from` date NOT NULL, `date_to` date NOT NULL, `reservation_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `expired` int(1) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) )
Now, let's insert some records to make a simulation. We will update the expired
field with value 1
if today is after date_to value
; otherwise, it will be 0
.
These are the records to insert in the reservation
database table:
INSERT INTO `reservation` (`id`, `room_id`, `customer_id`, `price_per_day`, `date_from`, `date_to`, `reservation_date`, `expired`) VALUES (1, 2, 1, 90.00, '2015-02-10', '2015-05-23', '2015-05-24 22:45:37', 0), (2, 2, 1, 48.00, '2019-08-27', '2019-08-31', '2015-05-24 22:45:37', 0), (3, 1, 2, 105.00, '2015-09-24', '2015-10-06', '2015-06-03 00:21:14', 0), (4, 1, 2, 150.00, '2015-06-22', '2015-06-28', '2015-06-21 22:24:25', 0), (5, 1, 2, 150.00, '2015-07-22', '2015-08-28', '2015-06-21 22:24:34', 0);
Now, create a new console controller in console/controllers/ReservationsController.php
with the following content:
<?php namespace consolecontrollers; use yiiconsoleController; /** * Manage reservations */ class ReservationsController extends Controller { /** * Update 'expired' field of reservations */ public function actionUpdateExpired() { $models = commonmodelsReservation::find()->all(); foreach($models as $m) { echo sprintf('Check reservation #%d - date_to = %s - status : %s', $m->id, $m->date_to, (strtotime($m->date_to)<=time())?'OK':'Expired'); echo " "; // Set expired field. I'll for every model because if we could have changed 'date_to' value. $m->expired = (strtotime($m->date_to)<=time())?0:1; $m->save(); } // equivalent to return 0; return Controller::EXIT_CODE_NORMAL; } } ?>
In actionUpdateExpired
, we display for each model some data to the console, such as id
, date_to
, and status
. Then, we will set for each model the value of the expired
field, based on the date_to
value.
Finally, we will launch this command:
$ ./yii reservations/update-expired
This will return the following output:
Check reservation #1 - date_to = 2015-05-23 - status : OK Check reservation #2 - date_to = 2019-08-31 - status : Expired Check reservation #3 - date_to = 2015-10-06 - status : Expired Check reservation #4 - date_to = 2015-06-28 - status : OK Check reservation #5 - date_to = 2015-08-28 - status : OK
3.145.143.239