start-gii.md 4.72 KB
Newer Older
1 2 3
Generating Code with Gii
========================

Qiang Xue committed
4 5 6 7 8 9 10 11 12 13 14 15
In this section, we will describe how to use [Gii](tool-gii.md) to automatically generate the code
that implements some common features. To achieve this goal, all you need is just to enter the needed
information according to the instructions showing on the Gii Web pages.

Through this tutorial, you will learn

* How to enable Gii in your application;
* How to use Gii to generate an Active Record class;
* How to use Gii to generate the code implementing the CRUD operations for a DB table.
* How to customize the code generated by Gii.


Qiang Xue committed
16
Starting Gii <a name="starting-gii"></a>
Qiang Xue committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
------------

[Gii](tool-gii.md) is provided by Yii in terms of a [module](structure-modules.md). You can enable Gii
by configuring it in the [[yii\base\Application::modules|modules]] property of the application. In particular,
you may find the following code is already given in the `config/web.php` file - the application configuration,

```php
$config = [ ... ];

if (YII_ENV_DEV) {
    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = 'yii\gii\Module';
}
```

The above configuration states that when in [development environment](concept-configurations.md#environment-constants),
the application should include a module named `gii` which is of class [[yii\gii\Module]].

If you check the [entry script](structure-entry-scripts.md) `web/index.php` of your application, you will
find the following line which essentially makes `YII_ENV_DEV` to be true.

```php
defined('YII_ENV') or define('YII_ENV', 'dev');
```

Therefore, your application has already enabled Gii, and you can access it via the following URL:

```
http://hostname/index.php?r=gii
```

48 49
![Gii](images/start-gii.png)

Qiang Xue committed
50

Qiang Xue committed
51
Generating an Active Record Class <a name="generating-ar"></a>
Qiang Xue committed
52 53
---------------------------------

54
To use Gii to generate an Active Record class, select the "Model Generator" and fill out the form as follows,
Qiang Xue committed
55 56 57 58

* Table Name: `country`
* Model Class: `Country`

59 60
![Model Generator](images/start-gii-model.png)

Qiang Xue committed
61 62 63 64 65 66 67
Click on the "Preview" button. You will see `models/Country.php` is listed in the result.
You may click on it to preview its content.

Because in the last section, you have already created the same file `models/Country.php`, if you click
the `diff` button next to the file name, you will see the difference between the code to be generated
and the code that you have already written.

68 69
![Model Generator Preview](images/start-gii-model-preview.png)

Qiang Xue committed
70 71 72 73 74
Check the checkbox next to "overwrite" and then click on the "Generate" button. You will see
a confirmation page indicating the code has been successfully generated and your existing `models/Country.php`
is overwritten with the newly generated code.


Qiang Xue committed
75
Generating CRUD Code <a name="generating-crud"></a>
Qiang Xue committed
76 77 78 79 80 81 82 83
--------------------

To create CRUD code, select the "CRUD Generator". Fill out the form as follows:

* Model Class: `app\models\Country`
* Search Model Class: `app\models\CountrySearch`
* Controller Class: `app\controllers\CountryController`

84
![CRUD Generator](images/start-gii-crud.png)
Qiang Xue committed
85

86
Click on the "Preview" button. You will see a list of files to be generated, as shown below.
Qiang Xue committed
87

88 89 90
Make sure you have checked the overwrite checkboxes for both `controllers/CountryController.php` and
`views/country/index.php` files. This is needed because you have already created these files
in the previous section and you want to have them overwritten to have full CRUD support.
Qiang Xue committed
91 92


Qiang Xue committed
93 94
Trying it Out <a name="trying-it-out"></a>
-------------
Qiang Xue committed
95 96 97 98 99 100 101 102 103 104 105 106 107

To see how it works, use your browser to access the following URL:

```
http://hostname/index.php?r=country/index
```

You will see a data grid showing the countries in the database table. You may sort the grid
or filter it by entering filter conditions in the column headers.

For each country displayed in the grid, you may choose to view its detail, update it or delete it.
You may also click on the "Create Country" button on top of the grid to create a new country.

108 109 110 111
![Data Grid of Countries](images/start-gii-country-grid.png)

![Updating a Country](images/start-gii-country-update.png)

Qiang Xue committed
112 113 114 115 116 117 118 119 120 121 122 123
The following is the list of the generated files in case you want to dig out how these features are implemented,
or if you want to customize them.

* Controller: `controllers/CountryController.php`
* Models: `models/Country.php` and `models/CountrySearch.php`
* Views: `views/country/*.php`

> Info: Gii is designed to be a highly customizable and extensible code generation tool. Using it wisely
  can greatly accelerate your application development speed. For more details, please refer to
  the [Gii](tool-gii.md) section.


Qiang Xue committed
124
Summary <a name="summary"></a>
Qiang Xue committed
125 126 127 128
-------

In this section, you have learned how to use Gii to generate the code that implements a complete
set of CRUD features regarding a database table.