Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
58967f13
Commit
58967f13
authored
May 10, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
guide WIP [skip ci]
parent
cd162d9b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
119 additions
and
2 deletions
+119
-2
start-gii.md
docs/guide/start-gii.md
+118
-1
Generator.php
extensions/gii/generators/crud/Generator.php
+1
-1
No files found.
docs/guide/start-gii.md
View file @
58967f13
Generating Code with Gii
========================
> Note: This section is under development.
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.
Starting Gii
------------
[
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
```
Generating an Active Record Class
---------------------------------
To use Gii to generate an Active Record class, select the "Model Generator". You will see the following page:
Fill out the form as follows:
*
Table Name:
`country`
*
Model Class:
`Country`
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.
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.
Generating CRUD Code
--------------------
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`
Click on the "Preview" button. You will see a list of files to be generated, as shown below.
Make sure you have checked the checkbox for the
`controllers/CityController.php`
line. This is needed
because you have already created this file in the previous section and the file needs to be overwritten
to have full CRUD support.
How It Works
------------
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.
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.
Summary
-------
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.
extensions/gii/generators/crud/Generator.php
View file @
58967f13
...
...
@@ -110,7 +110,7 @@ class Generator extends \yii\gii\Generator
'indexWidgetType'
=>
'This is the widget type to be used in the index page to display list of the models.
You may choose either <code>GridView</code> or <code>ListView</code>'
,
'searchModelClass'
=>
'This is the name of the search model class to be generated. You should provide a fully
qualified namespaced class name, e.g., <code>app\models\
search\
PostSearch</code>.'
,
qualified namespaced class name, e.g., <code>app\models\PostSearch</code>.'
,
]);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment