4

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!

tacos_tacos_tacos
  • 10,277
  • 11
  • 73
  • 126
  • I'm on my third Yii project, in the first one I used Gii, in the second one Giix, but now I have used Giix just to generate the models and have them in another folder just to use it as reference. So when during the development process I need to create a Model, I take some code from the generated model and I write the rest. – Puigcerber Mar 28 '12 at 07:26

3 Answers3

7

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.

Rohit
  • 238
  • 2
  • 6
4

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.

Jason
  • 1,726
  • 17
  • 19
1

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.

sdlins
  • 2,195
  • 1
  • 23
  • 31
  • So the @property declarations don't really do anything? – tacos_tacos_tacos Mar 28 '12 at 05:37
  • @property is just the phpDoc. It is very useful for documentation of your system, sure, and for IDEs to know how to make code completion. – sdlins Mar 28 '12 at 06:03
  • OK, but it doesn't actually do anything? Sadly that is news to me... I always had thought it was responsible somehow for the magic going on in the background... – tacos_tacos_tacos Mar 28 '12 at 06:12
  • 1
    in model you will still need to add validation to this new field because its *unsafe* until you do that. I think its better to think of Gii as starting helper and after that you by your own. Design your table with pencil/paper (or some diagram program) and when generate with Gii when you think you ready. If some field comes its just one field, no big deal - change it manualy :) – briiC Mar 28 '12 at 06:23