Should I continue to rely on code generation to generate my models and CRUD?

As I delved into Yii, I now wonder if I rely on Gii and Giix to rely on my models, and the β€œadmin” CRUD can be a crutch, not a time saver. Many times in the initial stages of small projects, this helps me accelerate, allowing me to focus on database design. However, whenever I make changes to the structure of a table or relationship, I have to rely on GiiX to re-generate the model. Before I do this, I always copy the part of the model that I wrote in order to subsequently paste it into the updated model. This seems like a tedious thing, and now I am wondering if I am saving any actual time. I have a few questions:

  • For Yii users, when you do Yii for a while, are you even worried about Gii or GiiX? Did you stop using it because it was no longer useful, or because it was a crutch? Have you worked on writing your own code generation and scaffolding tools?
  • For all coders, do you think you should avoid creating code when learning a new language or framework?

My hope is that there is an effective way to use Gii and other code generation tools even after repeatedly updating the table structure and writing to my own code without copying and pasting and tracking what.

Please let me know your thoughts!

+4
source share
3 answers

I do not know if this is possible with Yii, but with another map that I use, we extend the model classes and put our custom code in these extended classes. In the application, we refer only to the extended class, and not to the base (generated) model classes.

Since we do not put any user code in the classes of the base models, they can be generated without worrying about overwriting any user code.

+4
source

Gii is useful for generating template source code and directory structure.

As the project progresses, I use the diffs provided by Gii to add the corresponding new code snippets to the files of my model class. Let's say you change the table. Go to Gii and try creating a model. You will receive a notification that the model class file exists. In addition, you will see a link that gives you the difference in the popup.

+7
source

However, when I make changes to the structure or relationships of the table, I have to rely on GiiX to re-generate the model.

You really don't need it. The Yii construct makes all of your table fields available as attributes in your model. Thus, if you add a new field X to your TableA, you can imediatelly use $ modelA-> fieldX. You do not need to make updates in your model. Yii β€œknows” that you have changed the table.

Cm:

β€œAlthough we never explicitly declare the title property in the Post class, we can still access it in the code above. This is because the name is a column of the tbl_post table, and CActiveRecord makes this available as a property using PHP __get () magic Method : An exception will be thrown if we try to access a non-existent column in the same way. "

Source: http://www.yiiframework.com/doc/guide/1.1/en/database.ar

For Yii users specifically, once you've been doing Yii for a while do you even bother with Gii or GiiX? Did you quit using it because it was no longer useful, or because it was a crutch? Did you work on writing your own code generation and scaffolding tools? 

I use Gii in all my projects for most models or the CRUD generation. It is very useful. I can customize the generated code the way I want. I even made some settings for the "skeleton" of the Gii generator, so the code was created in my language, not in English, and with some methods / attributes I need more.

 For all coders, do you feel code generation tools should be avoided when learning a new language or framework? 

No, IMO. Generated code is another way to find out.

+1
source

Source: https://habr.com/ru/post/1403804/


All Articles