CRUD operation With Gii and The Yii2 Framework

Published on Author Code Father
CRUD operation With Gii and The Yii2 Framework.

What is Gii? Gii is an incredible piece of software that is part of the Yii Framework. It is a wonderful tool that writes code for you. So many of the development requirements of web applications in PHP include repetitive code, which is why we have these frameworks in the first place. The Gii tool takes it a step further, and creates the code needed to power Create Read Update and Delete functions, new Active Record Models, new Controllers, Forms, Modules, and extensions. It really is incredible. If you have been following along so far in this Yii Tutorial Series, you already have Gii installed and you’re ready to go. Let’s check it out.

How to Program With Yii2:

Confirm Gii Is Installed

Like we mentioned, if you created a project along with us, you already have Gii installed. Visit the following URL to check.

http://localhost/yii/basic/web/index.php?r=gii

yii gii code generator The code that enables this is found in config/web.php and web/index.php.

config/web.php

web/index.php

Create A New Model

Now we can create a new model using Gii. First up, we need a table to work with. We’re going to create a simple no nonsense database table that will hold information about guitars. It will have an ID field, brand, and model. Here is the code to generate that new table.

Now that we have our table, we’ll choose the option to generate a Model in the Gii tool. Note that you provide the exact name of the table to generate a model for in lower case for the table name field. The Model Class will be the exact same name with just the first letter in uppercase. You must follow this convention. We will uncheck the option to generate relations on this first pass through, since we don’t have any. This is just a plain vanilla table with 3 basic fields. yii guitars model

models/Guitars.php source

Awesome! We did not write even one line of code, all we did was tell Gii the name of the table, and now we have a fully functioning Model.

Yii2 Tutorial For Beginners:

Create A Form With Gii

Gii can now create a view file for you based on the associated Model you just created. Note: When you visit the form generator, if you do not provide the fully qualified namespace for the Model Class field, you will get an error like Class ‘Guitars’ does not exist or has syntax error. So in our case, instead of specifying Guitars as the Model Class, we use appmodelsGuitars and all is well. yii gii form generator When this task is completed, you will get the following message:

The form has been generated successfully.You may add the following code in an appropriate controller class to invoke the view:

Fantastic! Not only did Gii create our view for us, it gave us the code that we’ll need for our controller as well. Here is the generated view code:

guitars.php source

Create The Controller

One thing that will throw a few people off no doubt is the conventions of having use statements at the top of the controller file versus referencing the full namespace path then instantiating objects. Note the code we listed above used the second approach, but having use statements is a little more clear. We’ll create our controller as such.

GuitarsController.php source

Do note that if you get an error such as PHP Fatal Error – yiibaseErrorException Class ‘appcontrollersYii’ not found this just means you forgot to include use Yii; in your namespace declarations, so make sure to include that like you see above. At this point, we should have a functioning model, view and controller. Since this is an instance of ActiveForm, it will also have that super slick client side validation included as well. We can test it out by loading up the controller/view association. We’ll submit several guitars too.

http://localhost/yii/basic/web/index.php?r=guitars/guitars

yii guitar form After we insert a few guitars, we can check out the results by looking in our database to be sure they took.

Oh Yeah – It’s working perfectly.


The Gii CRUD Generator

So far we’ve been doing the code generating in a somewhat piecemeal way. With Gii, we can do an entire CRUD scenario for tables in our database. In this go around, we’ll create a drums table and let Gii create the entire setup for us.

1. Create The Drums Table

2. Create the Model

You must create the Model for your table before you can run the CRUD generator. In this case, proceed exactly as we did with guitars, just use drums instead.

3. Run the CRUD Generator

yii gii crud generator

4. Enjoy Fully Functional CRUD!


Create

http://localhost/yii/basic/web/index.php?r=drums/create

gii create


Read

http://localhost/yii/basic/web/index.php?r=drums/index

gii read


Update

http://localhost/yii/basic/web/index.php?r=drums/update&id=2

gii update


Delete

http://localhost/yii/basic/web/index.php?r=drums/delete&id=2

gii delete


Wow! This should give you an idea of the power contained in Yii and it’s code generation tool Gii. All you need is a properly defined database table, and Gii will generate the entire CRUD infrastructure needed to give you a base functioning application. Super impressive.


The Generated Code

In the prior section, we saw how easy it was to create the full suite of crud functionality with Gii. All we did was create a database table, use Gii to generate a model, then used Gii to generate the entire crud code. Let’s look at the code generated so we understand how it works. This is necessary since the next step in working with and creating Yii applications is to customize the generated code by hand to meet the requirements for your website or application. See if you can understand exactly how these files interact. If you are familiar with the Model View Controller paradigm, it should be fairly straightforward.


Model(s)

models/Drums.php source

models/DrumsSearch.php source


Controller

controllers/DrumsController.php source


View(s)

views/drums/create.php source

views/drums/_form.php source

views/drums/index.php source

views/drums/_search.php source

views/drums/update.php source

views/drums/view.php source

Conclusion

In this episode we checked out the awesome Gii Code Generation tool which is a part of the great Yii 2 Framework. We learned that by simply providing some basic information to the tool, along with a valid database table, it will create all the code we need to interact with that data.

Comments

comments

Categories PHP

One Response to CRUD operation With Gii and The Yii2 Framework