Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

You really do not need that. Yii design makes all of your table fields available as attributes in your model. This way, if you add a new fieldX to your TableA, you can imediatelly use $modelA->fieldX. You do not need make any upgrade in your model. Yii 'knows' you have changed the table.

See:

"Although we never explicitly declare the title property in the Post class, we can still access it in the above code. This is because title is a column in the tbl_post table, and CActiveRecord makes it accessible as a property with the help of the PHP __get() magic method. An exception will be thrown if we attempt to access a non-existing 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 of my projects for the most of models or CRUD generation. It is very useful. I can customize the generated code the way i want. I even have done some customizations to 'skeleton' of Gii generator so that the code generated to be in my language, not 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. The generated code is one more way to learn.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top