Yii Gii - Yii

What is Yii Gii?

Gii is the extension, facilitating a web-based code generator for generating models, forms, modules, CRUD, and so forth.

By default, the generators available are −

  • Model Generator − Generates an ActiveRecord class for the specified database table.
  • CRUD Generator − Generates a controller and views that implement CRUD (Create, Read, Update, Delete) operations for the specified model.
  • Controller Generator − Generates a new controller class with one or several controller actions and their corresponding views.
  • Form Generator − Generates a view script file that displays a form to collect input for the specified model class.
  • Module Generator − Generates the skeleton code needed by an Yii module.
  • Extension Generator − Generates the files needed by a Yii extension.

Gii generator tool is open by entering http://localhost:8080/index.php?r=gii: in the address bar of the web browser and it appears as:

Generation Tool

Preparing the DB

Step 1 − A new database is created. Database can be prepared in the following two ways −

  • In the terminal run mysql -u root –p
  • Create a new database via CREATE DATABASE helloworld CHARACTER SET utf8 COLLATE utf8_general_ci;

Step 2 − The database connection is configured in the config/db.php file. The following configuration is for the system used currently.

Step 3 − Inside the root folder run ./yii migrate/create test_table. This command will create a database migration for managing our DB. The migration file should appear in the migrations folder of the project root.

Step 4 – The migration file m160106_163154_test_table.php is modified by the code:

A user table is created with the above migration with these fields: id, name, and email and few demo users are added.

Step 5 − Inside the project root run ./yii migrate to apply the migration to the database.

Step 6 − A model is created for user table. For the sake of simplicity, use the Gii code generation tool. Open up the url: http://localhost:8080/index.php?r=gii. Then, click the “Start” button under the “Model generator” header. Fill in the Table Name (“user”) and the Model Class (“MyUser”), click the “Preview” button and finally, click the “Generate” button..

Create Model

The MyUser model should appear in the models directory.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Yii Topics