Yii 2 Gii code generation

One of things about Yii 2 that is drastically different from some of the other frameworks is Gii, it’s code generation tool. I cover the use of Gii extensively in my book, Yii 2 For Beginners.

On the surface, this might just seem like a fun feature to use, a time-saver in certain circumstances. And that’s probably true of a lot of code generators, they only take you so far. But Gii is different.

Gii is so powerful that it has very meaningful impact on work flow. And this impact reaches deep into our profession. You can look at it from two perspectives, one, the individual developer doing work for clients, and two, from a team perspective, which can bring you up to full enterprise level.

So let me talk first about how it impacts the individual, and since a lot of my readers are beginners, it will help them understand the importance of Yii 2 and Gii.

1. Planning. Use of Gii impacts planning. Before you code a single line of code, you can factor in not only the efficiency of Gii, but also the workflow.

For example, to start, you look at the scope of the project and begin sketching out the table structure. The more well-thought out this data-structure is, the useful Gii becomes. If you use foreign keys to relate tables, Gii will understand this and generate relationships in the models.

This gives you the ability to imagine the project, and better yet, diagram the tables in MySql workbench, before any PHP is involved. And once you know the number of tables you are working with, you will have some idea of how long basic development will take. This is possible because you can average how long each table takes to convert to model to controller to view.

In a typical implemenation, there are 8 files generated. And they also typcially need customization, but once you have an idea of what you generally use for that, even the 1st level of customization goes quickly. When I say quickly, I mean crazy fast. It’s awesome. Awesome is not a technical word, but you have to give me a pass on that one, I really love Gii.

2. Strong MVC Pattern. The code that Gii generates is a beautiful implementation of the MVC pattern. It’s probably the easiest structure to follow of all the frameworks because you literally get folders named models, controllers, and views. And once you understand how it’s all stiched together, it just makes perfect sense.

3. Learn Good Practices. As your workflow and project planning are impacted by Gii, you begin to develop a discipline to the structure. While you are certainly not locked into anything, if you follow the breadcrumbs of good practices that Yii 2 and Gii are putting out there for you, your project becomes very tight and focused. Things are always done the same way, at least at the base level.

It’s understood of course that I’m talking about the bones of the project, not the skin. Presenting the data in the views often requires a lot of customization, but of course Yii 2 helps you bigtime there too with its widgets. But the widgets will have to be the subject of another post.

4. Grow As A Developer. As an individual developer, you build value into your skill set by following best practices. Today you are an individual developer or student. Tomorrow, you may work in a team environment. And those looking to hire programmers are looking for programmers that can follow a discipline and they’re most likely looking for programmers who have framework experience. And the good news is that this is a very accessible framework. You can learn it and benefit from it.

And that gives us the perfect transition to talk about team development. All the benefits that individual developers get from using Yii 2 and Gii are amplified in team and enterprise development. While time estimates are also important to individual developers, they can be absolutely critical in a company environment, where commitments are made to clients or to the backers of a venture.

So you can look at the list of benefits like this:

1. Workflow Standardization. The planning and flow of the project always follows the same path. Team members are assigned specific roles, which is certainly not unusual, but knowing the power of the framework and the capabilities of Gii enhances the strategic deployment of those resources.

2. Common Starting Point For Code. If the conversation starts with data-structure, it will lead to using Gii, in most cases. Obviously there are exceptions to everything. But to the extent that Gii gives us a common starting point for code, it gives the security of building a proven structure. The tight association it creates between model, controller, and view is not something you want to go out of your way to break, unless you have very good reason to do so.

3. Rapid Prototyping. Gii gives you the ability for rapid prototyping. Companies rarely have the luxury of asking “what if?” But with Gii, you can do that. You can even set aside a prototype branch with a separate DB, and playout those whatif scenarios quickly. No company would ever do that unless they could do it quickly and efficiently. But having that ability can give you a competitive advantage, so this is great.

4. Security Of Standardization. Lastly, I should state one of the most obvious benefits to the standardization that both Yii 2 and Gii offer. What if someone leaves the team? Someone else will have to take over their responsibilities, and wouldn’t it be better to know that they can do that efficiently because the structure and discipline of Yii 2 and Gii were adhered to? Of course it would. That’s a big reason why companies use frameworks in the first place.

Besides all the reasons I stated about the benefits of using Yii 2 and Gii, I thought about why it is so good at what it does and why I haven’t seen a competing framework offer something as compelling, at least not from my point of view. And I think the answer is that Yii 2 has it’s own native ORM implementation, which just makes their code generation seemless.

Having their own native ORM also gives Yii 2 the freedom to structure their MVC implementaion how they wish, so they’re able to keep it intuitive, without having to make sacrifices because of 3rd party ORM or other considerations. I believe this is a huge advantage and is one reason why Yii 2 is able to offer so much out of the box. If you haven’t tried this framework, I would strongly encourage you to do so.

I’d like to take a moment and thank all the programmers from around the world who are reading my blog, I hope you are finding it useful. And thanks again to everyone who has supported my book. Feel free to contribute with your own comments, links, and reviews, it is greatly appreciated.

5 thoughts on “Yii 2 Gii code generation”

  1. Have you spent time on customising the code templates in Gii? Can you really modify them infinitely to generate the skeleton code as you want? I am for example interested in extracting validations from configuration files (beyond simple string length, mandatory vs optional) and having that also be in the generated code. Perhaps if you have some insight an article on the customisation of Gii would be great?

    1. Thanks for your question. My work is oriented to beginners, so we don’t go over customizing Gii templates, since that is not a beginner topic.

      However I have reviewed the Guide and you can see they list clear instructions for how to build and use your own templates with Gii. They even provide a code example. You can view that here.

      Also, there is a robust community project named giix, which gives extended functionality, which you might find useful.

      You can also easily build your own custom validators, so I don’t see any reason why you couldn’t include them in your custom templates.

      I hope you find the above links useful. Thanks again for your comment.

  2. Bill,

    I bought you book. I’m very happy that I found a guide to cover the advanced version of Yii vs the basic. I have a question around the models.

    I noticed the database models extend active record, and the LoginForm/ContactForm extend Model. However the term models is being used for both. From an obvious standpoint I know the ActiveRecord models relate to the database, but I cannot figure out why the forms extend Model.

    Could you clarify why they both considered models?

    Also, finally how does the web/User and common/model/User relate to each other? Or do they not?

    Thanks again for writing this, I’m at page 145 and look forward to finishing the second half.


    1. Hi Kunal, thanks for your kind words about the book. In the MVC pattern, the model controls the business logic, and in Yii 2’s implementation, Form models are separate models. This makes a lot of sense if you think about it because it keeps a lot of business logic out of the controller, which needs to remain thin.

      In many cases, you will see the form model call an instance of the ActiveRecord model, so they are closely related. Also, you won’t need a separate form model, unless there is a more complicated data manipulation to be done. If you look at the form models that Yii 2 provides, Contact, Login, ResetPasswordForm, you can see implementations of form models and how they differ from the other models that extend ActiveRecord.

      The user model in common/model/user is obviously not the same as the one in web/User. That confused me a little as well when I first saw it, but you have to remember that Yii 2 is a huge framework and many of the features reside in the framework layer that’s in the Vendor folder. The easiest way I can describe it is that when you utilize the provided user model in common/models, you are also utilizing methods from web/user via the controller. This subject gets deep and complicated quickly. It’s not really described in the guide in detail and I only know about it because I traced the declarations, probably as you have, to see how everything connected. It’s not really a beginner subject, so I don’t go into more detail on it. We focus on the layer that gets you up and running.

      I hope my answers are useful to you and I hope my book helps you reach your goals with Yii 2. Please note I will be expanding the book in the near future with more material, so look for announcements on that. You get free updates for the life of the book and I will be actively developing it for the foreseeable future. I always advise people to get the latest version whenever they can, and on the Leanpub.com page, they always show the last update date, so you can easily check to see if you have the latest copy.

      Thanks again for your positive comments on the book, as always, any comments, links and reviews are much appreciated.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s