In the last installment of this series, I covered a simple contact form app powered by Yii2. In this article I will discuss Gii (a Yii2 module) and create a CRUD capable application using Gii.
What is Gii
Gii is a web-based code-generator module provided by Yii framework. It helps you create and generate fully customized forms, models, CRUD for database and more. To access Gii in Yii2, just route your url to index.php?r=gii.
To demonstrate the capabilities of Gii, I will create a Model and CRUD for a to-do list using Gii. Let’s get started.
Creating a Table for To-do list:
Let us first create a new table in the same database that we used in the previous article. This table will be used to save to-do items. Schema for the table is:
CREATE TABLE `todo` ( `id` int(11) NOT NULL, `name` varchar(50) NOT NULL, `description` text NOT NULL, `duedate` date NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
In order to create a CRUD for this table, we need to create its model first. Now, we don’t need to write the code for it; I will be using Gii Model Generator for it. Let’s do it.
Generating Module Using Gii
Now open Gii in the browser and click Start under Model generator.
A form will open in which will ask the table name and model class. In the table name type t and the table name will automatically be suggested with the reference letter.
After selecting the table, press Tab and a class with the same name as the table will be automatically added in the text box.
Leave the other options as they are and click preview button. Once you have clicked the preview, you will see what files will be generated. Since we are only making a model class, only one file will be generated.
Once the process finishes, you will see the message that your code has been generated successfully.
Now head to the model folder and you will see a new model Todo is generated.
Next, let’s create a CRUD for this model.
Generating CRUD using Gii
Now on the page where we created the model, click CRUD Generator.
Fill the form that opens.
- Add the model class for which we are generating CRUD. This requires the full path of the model that must have been generated beforehand. For now, use
- Next, the model for the search classis required. Since this class has not been generated yet, just name it and the CRUD generator will generate this class automatically. Let’s call the class TodoSearch. The path of this model is
- The controller that will be generated also requires a name. Let’s call this controller TodoController. The path for the controller is
- Last the view for the app must also be named. The path for the view is
Now click Preview.
A look of the files that would be generated confirms that in addition to the Controller and the Search model, all the necessary views for the CRUD operations will also be generated. Click Generate to complete the process.
Now that all the files have been generated, go to the Yii directory and you will find that all the code has already been generated.
Now let us test the newly created app by executing CRUD operations.
Creating And Updating the To-do List
Let’s test the app by going to the controller index: ?r=todo/index.
This page will show a blank list, since we haven’t added any items yet. Let us insert a new item by clicking the Create Todo button.
Notice that Duedate is text. This needs to be changed to date so that the users could pick a date from the date widget and the app could save the input in the proper format. For this, go to the todo folder inside the views and open _form.php. Change this line of code:
<?= $form->field($model, 'duedate')->textInput() ?>
<?= $form->field($model, 'duedate')->Input('date') ?>
Now save the file and refresh the page. You will see that input type has been change to date.
Now let’s add a new to-do event using this form. Once you have added the todo, you will be redirected to it’s view page.
Let’s say, the item added needs to be updated. Just click the update button and a form will open.
When you want to see the complete list of items, just go to the main index page, ?r=todo/index.
If you want to delete an item from the main list, just click the Delete Icon button.
In this article I discussed Gii and how it works with Yii2. I elaborated by developing a simple CRUD capable app to highlight the working of Gii on Yii2 framework. In the fifth part of this series, I will cover Redis Configuration in Yii2. Here is the complete list of topics covered in this series on Yii2.