Home | About | Web Stories | View All Posts

9 Apr 2022

How to generate CRUD files in Yii2 using Gii?

We can write the CRUD(Create, Read, Update, Delete) files quickly in Yii2 using the Gii application, for accelerating application development speed. We pass the different parameters into Gii CRUD generator for this.

Generating CRUD files in Yii2 framework

There are three steps for this -
1. Generating Model class (ActiveRecord class for the specified database table)
2. Generating controller and views files for above model.
3. Browsing newly created views pages

1. Generating Model class

Open phpmyadmin and create a table(eg. - department, having two fields - DId, DName) in the project database.



A new ActiveRecord class will be associated with this "department" table. Now browse the Gii page using your choice of location either at 'frontend' or 'backend'. For example url for CRUD generation in 'backend' -

"http://localhost/yii2cms2022/backend/web/index.php?r=gii"

The code generators list page will be shown as like below screenshot -



Now click at the "Start" button in the "Model Generator" section at the above code generator Gii page for opening the model generator form. You need to fill the appropriate entry eg.- table name etc. Below are the parameters entry for creating model class - "Department" for yii advanced template in the 'backend' location.

Database Connection ID:
db

Table Name:
department

Model Class Name:
Department

Namespace:
backend/models

Below is the Model generator form screenshot with entry -



You need to click at the above preview button for getting a code file path preview as below screenshot -



Finally you need to click at "Generate" button for writing the codes in the project folder. It will generate an ActiveRecord class code and a file at "C:\xampp\htdocs\yii2cms2022\backend\models" for the database table - "department". Below is the screenshot of the newly created file name and location -



2. Generating controller and views

Browse the Gii page again using the below sample url as we had done in the step one. You can also click at "Yii Gii Generator" logo for showing the generator list page.

http://localhost/yii2cms2022/backend/web/index.php?r=gii.

Now click at the "Start" button in the "CRUD Generator" section at the Gii page as like below screenshot for opening the CRUD generator form.



Below are the parameters entry for creating CRUD (Create, Read, Update, Delete) generator for a existing class - "Department" for yii advanced template in 'backend'.

Model Class :(Model must exist before start of the crud. Please generate the "Model" for Departments, before CRUD start.)
backend\models\Department

Search Model Class :
backend\models\DepartmentsSearch

Controller Class:
backend\controllers\DepartmentController

View Path:
@app\views\department
Note : you can use yii path alias for this. You can get path alias path using the codes at any view file like = (echo Yii::getalias('@app');, echo Yii::getalias('@web');.
@app will produce - H:\projectname\backend,
@web will produce - /projectname/backend/web
Best way - you should use physical path.
eg. - /wamp64/www/yii2cms/backend/web/themes/yourthemename/views/departments
Below will also work if you want to generate the views file in theme folder -
@webroot/themes/yourtheme/views/yourviewsname

Base Controller Class:
yii\web\Controller

Widget Used in Index Page:
GridView


Below is the CRUD generator form screenshot with entry -



You need to click at the preview button for getting a code file path preview as below screenshot -



Finally you need to click at "Generate" button for writing the codes in the project folder. It will generate model, controller and view code and files in the below shown path in your project. In my case project name is - "yii2cms2022".

Controller File path -
C:\xampp\htdocs\yii2cms2022\backend\controllers\DepartmentController.php

Search Model File path -
C:\xampp\htdocs\yii2cms2022\backend\models\DepartmentSearch.php

View folder path -
C:\xampp\htdocs\yii2cms2022\backend\views\department

View files path in 'department' view folder -
C:\xampp\htdocs\yii2cms2022\backend\views\department\_form.php
C:\xampp\htdocs\yii2cms2022\backend\views\department\_search.php
C:\xampp\htdocs\yii2cms2022\backend\views\department\create.php
C:\xampp\htdocs\yii2cms2022\backend\views\department\index.php
C:\xampp\htdocs\yii2cms2022\backend\views\department\update.php
C:\xampp\htdocs\yii2cms2022\backend\views\department\view.php

3. Browsing newly created views pages

Now first, we will create or enter the department name in this project database. Please use below sample url for that purpose -
Sample Url -
http://localhost/yii2cms2022/backend/web/index.php?r=department/create

In the aboe sample url - "department" is the controller name and "create" is the action function name(function actionCreate()) that has been written in the controller file - "DepartmentController.php". You can check more about URL routing in the Yii2 framework at "Know about route in Yii2 framework.".

Below is the screenshot for the department name entry or create form page.



Viewing the Department list -
Sample Url -
http://localhost/yii2cms2022/backend/web/index.php?r=department/index

In the aboe sample url - "department" is the controller name and "index" is the action function name(function actionIndex()) that has been written in the controller file - "DepartmentController.php". You can check more about URL routing in the Yii2 framework at "Know about route in Yii2 framework.".

Below is the screenshot for the department name listing page.



Tags :
Aashutosh Kumar Yadav

By Aashutosh Kumar Yadav

He is a PHP-based UI/Web designer and developer by profession and very interested in technical writing and blogging. He has been writing technical content for about 10 years and has proficient in practical knowledge and technical writing.
@www.infotokri.in

0 comments:

Post a Comment