Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

As I delve a little deeper into Yii I'm now wondering if relying on Gii and Giix to generate my models and "admin" CRUD may be a crutch rather than a time-saving tool. Many times in the beginning stages of small projects it helps me get going more quickly, allowing me to focus on database design. However, whenever I make a change to my table structure or relations, I find myself having to rely on GiiX to re-generate the model. Before I do so, I always copy the parts of the model that I have written so that I can paste it into the updated model later. This seems like a tedious thing to do and I'm now wondering if it is saving me any actual time. I have a few questions:

  1. 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?
  2. For all coders, do you feel code generation tools should be avoided 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 updating table structure multiple times and writing in my own code, sans the copying and pasting and keeping track of what's what.

Please let me know your thoughts!

like image 232
tacos_tacos_tacos Avatar asked Dec 16 '22 02:12

tacos_tacos_tacos


2 Answers

Gii is useful to generate the initial boilerplate code and directory structure.

As the project go ahead, I use the diffs provided by Gii to add the relevant new code snippets in my model class files. Say you modify a table. Go to Gii and try to generate the model. You will get notified that the model class file exists. Also, you will see the link that gives you the diff in a pop-up.

like image 168
Rohit Avatar answered Feb 15 '23 10:02

Rohit


I don't know if it's possible with Yii but with another framework that I use we extend the model classes and put our custom code into those extended classes. In the app we only reference the extended class, not the base (generated) model classes.

Since we don't put any custom code into the base model classes they can be re-generated without worrying about overwriting any custom code.

like image 40
Jason Avatar answered Feb 15 '23 11:02

Jason