Automatic code generation in Yii

Hi% username%! A few years ago, I met with the wonderful Yii framework, and since I have the opportunity to run a project on it, I fight for it.

One of the cool features that I really like is the Gii tool, which has been part of the framework since version 1.1.2. Now I want to talk about third-party modules based on it.

Gii "Out of the Box" generates a fairly functional code, often when you need to create a small feature you have to change very little. But what if we need more features? For example, add support for multiple languages, or change the style of the generated code?
There are 2 ways to expand: Modify existing templates and create your own generators (well, or use ready-made ones, respectively). Based on these capabilities, interesting enough products were created for comfortable work with auto code generation.

Giix


Extension page
Perhaps my favorite generator. Generates a base class with models for the models and a separately empty class of the model inheriting from the base. Thus, when changing the base, you can re-generate without overwriting your code.

It also generates a view with regard to connections, which very often saves a ton of time.

Key Features

  • Improved and simplified i18n
  • Support for HAS_MANY and MANY_MANY relationships.
  • Native support for saving MANY_MANY relationships using the new GxActiveRecord :: saveWithRelated method.
  • Correct processing of related model attributes, rendering of the corresponding form fields based on the type of relationships.
  • Convenient model structure for further work


Practice has shown that this generator, with a correctly designed database, generates a lot of the necessary code.

Manymanyajaxcrudadmin


Extension page
A pretty cool thing. Provides a template for generating code using ajax in the frontend for models with MANY_MANY bundles. It is convenient when you need to create an entity, for example, “product” or “category”.

For example, for the product we use the usual CListView or CGridView, and for categories we can use nestedset edited with the jstree plugin.

This whole thing is edited in modal dialogs, with the ability to attach files.
Based on Giix.

Conclusion


There are much more models and sets of templates for generation, I described only those that I had to use and which I really like.
The idea of ​​a fork for Giix does not leave me, there are really good ideas about this extension, for example: add setting field types before generation, setting up rules, filters, maybe it should be arranged as a new module for yii2. Time will tell.

This is my first article, before that I only knew how to troll. For trolling and insulting other users, earlier, I apologize. I ask you to write tips and criticism for writing the article in PM, with the same mistakes (my Russian is bad, I know, I'm working on it).

Links:
manymanyajaxcrudadmin
giix
Automatically generate code in Yii

Also popular now: