How to create Model in Yii2 using Gii tool

Create Model in YII2 using GII tool
Create Model in YII2 using GII tool

First of all, you should install yii2 to follow these steps.

After installation, you can then access Gii tool through the following URL:

[eg: http://localhost/yii2advanced/frontend/web/index.php?r=gii]

If you have enabled pretty URLs, you may use the following URL:

[eg: http://localhost/yii2advanced/frontend/web/index.php/gii]

possible errors: Page Not Found, Unable to resolve the request gii
solution: check whether the gii module is enabled in @app/frontend/config/main-local.php

if (!YII_ENV_TEST) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = 'yii\gii\Module';


Installing Gii Tool

Installing Gii Tool

Model Generator

This generator generates an ActiveRecord class for the specified database table.

Click “start” button under model generator to create Models.

Before that, create table in your database, for example

`product_name` varchar(50) NOT NULL,
`product_desc` varchar(100) NOT NULL,
`product_price` decimal(10,0) NOT NULL,
`created_on` datetime NOT NULL,

Table Name – This is the name of the DB table that the new ActiveRecord class is associated with.

Type 'products' in Table Name field

Model Class – This is the name of the ActiveRecord class to be generated.

Type 'Products' (starts with capital letter) in Model Class

Namespace – This is the namespace of the ActiveRecord class to be generated.

By default Namespace is app\models

Base Class – This is the base class of the new ActiveRecord class. It should be a fully qualified namespaced class name.

By default Base Class is yii\db\ActiveRecord

Database Connection ID – This is the ID of the DB application component.

By default Database Connection ID is db

Use Table Prefix – This indicates whether the table name returned by the generated ActiveRecord class should consider the tablePrefix setting of the DB connection.

For example, if the table name is tbl_post, the ActiveRecord class will return the table name as {{%post}}.

Generate relations – This indicates whether the generator should generate relations based on foreign key constraints it detects in the database.

Note: that if your database contains too many tables, you may want to uncheck this option to accelerate the code generation process.

Generate Labels from DB Comments – This indicates whether the generator should generate attribute labels by using the comments of the corresponding DB columns.

Enable I18N – Set this to true if you are planning to make your application translate into many languages.

Message Category – This category is used, if you enable I18N.

Code Template – Please select which set of the templates should be used to generated the code.

Then Click “Preview” button, it will show the path of the code file. If you already created this Model and you want to overwrite it. Check the overwrite check box.

Then click “Generate” button to generate the files and your Model files is generated in @app\frontend\models\

Create Model Using Gii

Create Model Using Gii

You may also like...

18 Responses

  1. Ruby Emeisel says:

    Best Tutorial for Yii…

  2. Keep posting articles related to Yii2…

  3. I like what you guys tend to be up too. This sort of clever work and coverage!
    Keep up the wonderful works guys I’ve incorporated you guys
    to blogroll.

  4. Besides its search results itself, nearly all people do not use Google’s other functions and tools which may have a standing of being
    more for your technologically inclined. The notion of
    organizing groups as circles is quite thoughtful. Below are a
    number of the focal points to provide a better understanding concerning
    how this new system will affect rankings.

  5. Please do video tutorials for better understanding…

  6. This is how I started out also it will be a great start for you as well.
    Greetings from YII developer.

  7. Something got me here.. Nice tutorial though..

  8. Lindsey says:

    Very Informative! Keep going.

  9. Paylaştığınız için teşekkürler

  10. Deborah says:

    Hi, Am also a developer. I want to create my own blog which theme are you using?

  11. I see you don’t monetize your page, don’t waste your traffic, you can earn additional cash
    every month because you’ve got high quality content.
    If you want to know how to make extra $$$, search for:
    Boorfe’s tips best adsense alternative

  12. Woah! I’m really enjoying the template/theme of this site.
    It’s simple, yet effective. A lot of times it’s very difficult to get that “perfect balance” between user friendliness and
    visual appearance. I must say that you’ve done a great job with this.
    Additionally, the blog loads super quick for me on Opera.
    Superb Blog!

  1. July 7, 2014

    […] you should provide the fully qualified class name of already created Model. eg: frontendmodelsProducts Note : If your model was generated in backend, put backend instead of […]

  2. March 28, 2018

    Cialis online

    There are certainly lots of details like that to take into consideration. That is a nice point to convey up. I provide the ideas above as general inspiration but clearly there are questions like the one you convey up the place a very powerful factor wi…

  3. April 11, 2018

    Thanks, Plenty of information.

Leave a Reply to Calvin Ziemba Cancel reply

Your email address will not be published. Required fields are marked *