superles / gii-template-collection

Automatically exported from code.google.com/p/gii-template-collection
0 stars 0 forks source link

Feature: separate generated models in base class + regular class #4

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
The idea is to make it possible to change the model late in development and 
regenerate it from the DB without erasing customizations.

Each model generates a base class, for example StudentBase in a StudentBase.php 
file, and a regular class, Student in a Student.php file. Student extends 
StudentBase.

If the user makes changes in his DB, for example adds a field, and re-runs gii, 
gii will replace MyObjectBase.php with the new model, and won't touch MyObject. 
This way, if you added custom methods in Student, say a 
getStudentsInClass($className) method, or overloaded default methods, you did 
this in Student.php which will not be overwritten.

In order not too have too many files in the models/ folder, we could have a 
models/base/ folder that contains the base classes, and keep the regular 
classes in models/

We also have to make clear, for example in base classes headers, that base 
files WILL be overwritten, and that the user should never write custom code in 
them.

For reference, this behavior is found is Symfony and is a quite handy thing to 
have.

Original issue reported on code.google.com by 2ben.da...@gmail.com on 4 Aug 2010 at 12:06

GoogleCodeExporter commented 8 years ago
Welcome 2ben.dahai.

This Idea is good and something i haven't thought about yet. This would use the 
Power of OOP a lot. Do you have a reference implementation ready for that? I 
don't think it would be that difficult, one just needs to choose which 
functions are being genereated into the Base classes, and which in the 
overloading classes, or am i missing something?

Original comment by thyseus on 4 Aug 2010 at 8:08

GoogleCodeExporter commented 8 years ago
Hi Thyseus,

I'm Alban by the way, I'm on IRC from time to time :) I'm glad you like the 
idea! I don't have a reference implementation now, but basically you're right, 
it's mostly just changing the current output file to Base (except maybe 
behaviors which would go to the overloading class), and generate the 
overloading class at first generation with almost nothing inside except the 
basic class declaration with the proper "extends MyObjectBase" statement.

I attach hereunder a sample class generated by Doctrine in Symfony following 
this idea. It's just to give an idea, not to turn gii into Doctrine ;)

Original comment by 2ben.da...@gmail.com on 5 Aug 2010 at 2:59

Attachments:

GoogleCodeExporter commented 8 years ago
please take a look at R71

http://code.google.com/p/gii-template-collection/source/detail?r=71

what do you think about that?

Original comment by thyseus on 5 Aug 2010 at 10:58

GoogleCodeExporter commented 8 years ago
OK I tried the generation and it seems to work well, then I tried to add one 
column to a table and regenerate, it seems to be working too :)
Maybe 2 small improvements:
- generate the base classes in a base sub-folder of models/ so it's a bit more 
tidy when writing code
- in the gii preview, could you make it that the base classes are preselected 
for overwriting ?

I'm very impressed to see this improvement has been done in such little time, 
this it really cool :) Thanks a lot !

Original comment by 2ben.da...@gmail.com on 6 Aug 2010 at 6:22

GoogleCodeExporter commented 8 years ago
Hello,

I support the idea of separating base classes in models/base, but then gtc must
ask the user to add an import in the configuration file, like

'import' => array(
    'application.models.base.*',
    ...
),

I have also opened issue 6 and issue 7 for other related issues.

And congratulations and thanks for gtc!

Original comment by rodrigo.coelho@gmail.com on 2 Sep 2010 at 7:59

GoogleCodeExporter commented 8 years ago
That's fixed, isn't it?

Original comment by schm...@usrbin.de on 9 Sep 2010 at 1:30

GoogleCodeExporter commented 8 years ago
It is not finished if the base models are going to be placed on models/base 
directory.

Original comment by rodrigo.coelho@gmail.com on 9 Sep 2010 at 1:49

GoogleCodeExporter commented 8 years ago
i renamed the base model from ModelBase to BaseModel so that the Base models 
are ordered together in the directory listing. This way we no longer need to 
have an extra separate base/ directory. It also avoids to have that additional 
'import' line in the application configuration. i propose to close this issue 
as it is.

Original comment by thyseus on 9 Sep 2010 at 2:24

GoogleCodeExporter commented 8 years ago
My vote is to separate base models to make it cleaner in projects with lots of 
models.

Original comment by rodrigo.coelho@gmail.com on 9 Sep 2010 at 5:33