SitePoint Sponsor

User Tag List

View Poll Results: If you had a good PHP code generation tool would you use it?

Voters
85. You may not vote on this poll
  • Of course, where can I download the source

    22 25.88%
  • Um, I would have to take a look at it

    50 58.82%
  • No, definitely not.

    11 12.94%
  • What's code generation?

    2 2.35%
Page 1 of 2 12 LastLast
Results 1 to 25 of 49
  1. #1
    SitePoint Enthusiast escape164's Avatar
    Join Date
    Dec 2002
    Location
    Colorado, USA
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Code Generation,

    Got a question for all of the Advanced PHP guys :-).

    I came across a project a while ago that generated PHP DAO's for me on the fly. The project was closed source, but I absolutely fell in love with it. I liked it so much that I went and implemented it myself.

    I use this DAO Generator all the time now. It finds itself into all of my projects and the fact that I don't have to write a DAO for each and every database table makes me very happy.

    I want to know if other people would be interested in something like this as well. I am thinking of starting a PHP code generation sourceforge project and posting the code, as well as a few other code generators that I have in mind.

    Now, one of the biggest things I have ran into when I show this to other people is that the classes that the code generator writes do not fit their style of coding, yadda yadda yadda. Suppose they did fit what you are used to, would you use them then?

    Thanks for the feedback in advance!

    *Edit*
    Some Links for further background Information
    PHP Patterns DAO
    Closed Source DAO Gen
    Java DAO Explanation
    Last edited by escape164; Nov 19, 2003 at 13:36.

  2. #2
    SitePoint Zealot spybreak's Avatar
    Join Date
    Apr 2003
    Location
    Germany
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what's a DAO?

    (sorry, newbie)

  3. #3
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Data Access Object

  4. #4
    SitePoint Zealot ZangBunny's Avatar
    Join Date
    Jul 2003
    Location
    Mainz, Germany
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First off, giving a link to DaoGen would have been nice, so everyone can take a look.

    DaoGen is a nice tool, if the code it generates matches the pattern you've had in mind for your application. Since it's closed source, you'd have to manually change the generated code otherwise, which kinda defies the purpose. So having a open source version of DaoGen would be really nice, and probably not all that difficult. It could probably be done by passing the data that the user inputs to a simple template that generates the PHP.

    What is even more interesting about having it open source is that it could be evolved into a nice O/R mapping package, something that even Martin Fowler in PEAA suggests one should buy rather than build the mapping layer by hand.

    If you're sourceforging it, count me in.

  5. #5
    SitePoint Enthusiast escape164's Avatar
    Join Date
    Dec 2002
    Location
    Colorado, USA
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I added the link above as well as some information on different links to what Data Access Objects are. I have a copy of the source and will attach it to a post here soon for people to download. I need to run through it and put names on who wrote what first though.

  6. #6
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    DAO Generation is probably the most useful / implementable of all the aspects that you could apply automatic php code generation to. So to answer your question, yes, I would use it provided that:
    1. The approach fits with my coding style
    2. The DAO's can be implemented with a number of DB abstraction layers, and can be extended
    3. The program / generator does not tie me to a single aspect of a rigid architecture, it just provides DAOs for me to use with whichever framework I wish.

    There are very few other "applications" of code generation (besides DAOs, that is) where I feel confident a competent solution could be developed. This translates into me saying that I would not use a code generation tool to create my business objects or any other framework classes in my application, or hardly anything else at all for that matter.

    So in sum, if done right, yes I would use it, but probably only for DAOs. Either way, I can't wait to see what you have!

  7. #7
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just went through generating a sample DAO with DaoGen, and while the intent is good, and it would be nice to have an open source version of it, there are a number of things I disagree with after examining the generated code:

    1. I don't quite like the way that most DAOs out there seem to try to force you into a getThis, setThat type of architecture. This means that for every new column you add to your DB / DAO, you have to modify your DAO and create a new Method. Unnecesary work in my opinion, never mind the fact that your class will grow much bigger than neccesary with a majority of methods that do nothing but blindy set or get a variable. While I don't condone direct access of private members, I would be much merrier with a single get/set(name, value) approach. If all of your DB data is held by an array in your object, as I happen to think it should be, then there is no loss of functionality or OOPability (there, a new word ) with this approach. Am I alone in this?

    2. I dont quite see the purpose of creating one object for every row that is retrieved from the DB. I think the DAO could be a store for any amount of data, one or many rows, but perhaps I am wrong in this. I would like for someone to show me the benefits (design wise) of doing it this way.

    3. The setAll function would be MUCH better served if it accepted a hash as a parameter, so the order of the variables was irrelevant. I think it is easy to see why this implementation would be more effective and flexible (as well as elegant, IMO).

    4. Why all the classes generated are independant and use no inheritance escapes me. In my mind a DAO architecture is about the most blatant target for proper inheritance, with a base class providing all the common methods (get, set, save, count, query... you name it), and the derived classes (UserDao, or DaoUser as I would have named it) only need to extend with class specific methods (ie: getAllUsersByStatus)

    Anyway, this is just one man's opinion, but if others agree, it might be good to consider some of these things in developing "the ultimate DAO"

  8. #8
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi.

    I have actually written a DAO/persistence generator (from XML using XSLT). They are extremely useful. Although the code generation part is easy, designing the underlying persistence mechanism certainly is not. It has taken myself and colleagues a couple of years (alongside other work) and yet ours is still crude. This is a big job. Have a look at some of the JDO implementations to see how big.

    Some issues...

    1) As has been pointed out, using getValue($name) is a possibility. You lose some security, but PHP method not found errors are unrecoverable whereas accessing a name that should not exist can be dealt with.

    2) Use the "generation gap" pattern (Vlissides). Makes testing a whole lot easier. Basically the objects should descend from a base class as was mentioned.

    3) You want to keep the actual code generation part as simple as possible. It is really difficult and time consuming to test thoroughly.

    4) Make sure they can easily be used with transactions. You will need separate commit() and confirmCommit() methods so that if the first commit fails the object is unchanged and can commit again.

    5) This is people's data. It has to be super tested and super reliable.

    Some future goals...

    1) Things get much more interesting with collections, that is one to many relationships.

    2) One to one and many to one are easiest done with joins. This is quite tricky, but possible.

    3) You also need to generate back-up and restore routines for the database. Something often forgotten. I recommend XML for this. You can then easily transfer data from one system to another and write schema porting scripts with XSLT.

    4) Avoid exposing primary keys and the like. This is tricky for many to one joins when reading from the database. It is still worthwhile though.

    The whole thing could become a huge job, but PHP desperately needs a decent object persistence mechanism, so it's an important job. Sadly PhpDo (Joe Walnes) is no more. Could this be the start of a replacement? Make sure you get plenty of volunteers.

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  9. #9
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One for the road: http://www.phpdb.org/ - haven't check this out but it's by one on the Binarycloud / Phing crew which gets me interested.

  10. #10
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wouldn't use Code Generation as I actually see no point for it as far as PHP is concerned in relation to PHP being an interperted language yes ?

    Would proberly be different if PHP was reduced to byte code ala Java for example by default ? But this isn't the case is it ?

  11. #11
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dr,

    I don't think that the benefits that could be drawn from code generation tools have much to do with interpreted vs compiled languages. Everybody needs to write the source code at some point, interpreted or not, and for certain design patterns (ie the DAO), the amount of repetitious coding can be sufficient to warrant an automated process. But of course, just like the choice of IDE, this is all in the eye of the beholder; some will think its a good idea, for some others it might not fit their development model.

    Anyone know what happened with Binarycloud? it seemed like it had a lot going for it, and then it got frozen in time, didnt it?

  12. #12
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi.

    Thanks for the link Harry. That is just the kind of thing I was thinking of.

    It is Java based though as they use the Torque persistence layer. It is not PHP native. They seem to have hidden this well, though, so if your development box has Java and the appropriate Apache components then you have the illusion of PHP persistence. If you haven't then you will have to have enough Java nonce to set all of this up. At that point you might as well have used a Java persistence layer and accessed it from PHP.

    My good doctor, PHP being interpreted makes code generation more desirable . Think of the code generation step as doing some of the processing by generating pre-calculated files.

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  13. #13
    SitePoint Zealot ZangBunny's Avatar
    Join Date
    Jul 2003
    Location
    Mainz, Germany
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ghurtado
    I don't think that the benefits that could be drawn from code generation tools have much to do with interpreted vs compiled languages. Everybody needs to write the source code at some point, interpreted or not, and for certain design patterns (ie the DAO), the amount of repetitious coding can be sufficient to warrant an automated process.
    Agree. Even PEAA says that writing the Data Mapping Layer by hand is tedious, repetitious work that can be done just as well by a good tool. While I'm against generating database code on the fly at runtime, using a tool to generate the groundworks and then fine-tune it seems like a good way to me. (The tuning should be done by inheriting and extending of course, so you won't get in trouble when your schema changes and you have to re-generate.)

    Quote Originally Posted by ghurtado
    Anyone know what happened with Binarycloud? it seemed like it had a lot going for it, and then it got frozen in time, didnt it?
    It was touted as the best thing since sliced bred all over Usenet and PHPBuilder by its creator (Alex Black), but I haven't seen any movement in the last year or so. I don't care for heavy frameworks too much, but I think Phing could have evolved into something useful...

  14. #14
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You may want to take a look at Metastorage.

    It is a tool that generates classes that act as API to store and retrieve data objects in databases.

    It is based on a Model Driven Architecture (MDA). This means that you just need to design your data model definition in a simple XML based format and it will generate classes that you will use in your application.

    It uses a JE WIN approach. ie it generates Just Exactly What You Need. This means that it won't generate generic code or fat base classes that bundle functionality that your particular model will not need. Therefore the generated code is optimized in size and for speed.

    Additionally, Metastorage can also generate optional classes that handle common types of Web based forms to act as user interface to manipulate your data objects. The forms are generated with compiled theme based templates. There are several familiar themes available, but you can build yours.

    As a bonus, Metastorage can generate class diagrams in the UML notation so you can integrate them in your project documentation or even show it to your clients. Clients often do not understand much of software, but UML diagrams helps them to understand what is being proposed. Often UML diagrams help clients accept and buy project bids that are being proposed.

    Metastorage is throughly documented but for a quick start, there is now a tutorial document in progress that is available here: http://groups.yahoo.com/group/metal-.../tutorial.html.
    Manuel Lemos

    Metastorage - Data object relational mapping layer generator
    PHP Classes - Free ready to use OOP components in PHP

  15. #15
    SitePoint Zealot ZangBunny's Avatar
    Join Date
    Jul 2003
    Location
    Mainz, Germany
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I browsed through the Metastorage documentation quickly, and one thing that struck me that it is tied to the Metabase database abstraction layer. This is fine if that is what you want to use, but otherwise can be a problem.

    (I could not get to the tutorial, as it is hidden in a yahoo group and you can't access it unless you join.)

  16. #16
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then I guess we should add to the list of requirements / desirable features that our DAO generator not be coupled with any particular DB abstraction layer, am I correct? Do you think this would be possible?

  17. #17
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ZangBunny
    I browsed through the Metastorage documentation quickly, and one thing that struck me that it is tied to the Metabase database abstraction layer. This is fine if that is what you want to use, but otherwise can be a problem.
    Metabase is just a quick way to support many databases. You can also use PEAR::MDB with Metabase wrapper to use the code generated currently by Metastorage.

    In the future, Metastorage generated code will not use any database abstraction package. Metastorage generated classes already provide any abstraction by themselves. Metastorage will generate only code that calls PHP native database functions and so achieve raw speed.

    Quote Originally Posted by ZangBunny
    (I could not get to the tutorial, as it is hidden in a yahoo group and you can't access it unless you join.)
    This is just a working version of the tutorial that was posted there for review of the MetaL development list subscribers.

    The access restriction was imposed by Yahoo. I cannot do anything about it. Anyway, you can have access if you want by subscribing to the group on this page: http://groups.yahoo.com/group/metal-dev/.
    Manuel Lemos

    Metastorage - Data object relational mapping layer generator
    PHP Classes - Free ready to use OOP components in PHP

  18. #18
    SitePoint Zealot ZangBunny's Avatar
    Join Date
    Jul 2003
    Location
    Mainz, Germany
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ghurtado
    Then I guess we should add to the list of requirements / desirable features that our DAO generator not be coupled with any particular DB abstraction layer, am I correct? Do you think this would be possible?
    I don't see why it shouldn't be possible. One purpose of having a Data Mapping Layer is that the whole layer can be swapped out at any time. If you want to switch from MySQL to Oracle, you just replace the MySQL layer with an Oracle layer.

    If the code generator supports multiple databases (sub-classing the code-generator for each RDBMS supported comes to mind), this can be done in a jiffy. That also means that any number of abstraction libraries can be supported just as easily as any database.

    Could this work?

  19. #19
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would think so. More to the point, the ideal code generator would be such that the code is neither specific to any single RDBMS nor DB abstraction layer. Perhaps coming up with an example of what kind of code we would like for it to generate, might solidify some of these ideas. Maybe I am way off, but I am thinking something like:
    PHP Code:
    class UserDao extends Dao
    {
        function 
    UserDao(){
            
    $this->defineTable('auth_users');
            
    $this->defineField('user_id''int''primary_key');
            
    $this->defineField('user_name''string');
            
    $this->defineField('parent_id''int''foreign_key');
        }

    Are we talking about something similar?

  20. #20
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ghurtado
    I would think so. More to the point, the ideal code generator would be such that the code is neither specific to any single RDBMS nor DB abstraction layer. Perhaps coming up with an example of what kind of code we would like for it to generate, might solidify some of these ideas. Maybe I am way off, but I am thinking something like:
    Metastorage generates database schemas in the Metabase schema XML format. The advantage of using Metabase is that it is capable not only of installing the schemas from the XML definition but also is capable of upgrading a schema by comparision with the version previously installed, in case you need to modify your schema.

    Metabase is capable of upgrading schemas without loosing any information of objects that were added or changed to the database after the schema was installed for the first time or changed for the last time.

    Metastorage generates a separate class just for doing schema installation but currently it just calls a single function of Metabase schema manager class.
    Manuel Lemos

    Metastorage - Data object relational mapping layer generator
    PHP Classes - Free ready to use OOP components in PHP

  21. #21
    SitePoint Guru
    Join Date
    Nov 2002
    Posts
    841
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A few thoughts on code generation...

    Quote Originally Posted by The Pragmatic Programmer
    1. Passive code generators are run once to produce a result. from that point forward the result becomes freestanding -- It is divorced from the code generator.

    2. Active Code generators are used each time their results are required. The result is a throw-away -- It can always be reproduced by the code generator.
    I have a definite preference for active code generators. These are things like compilers. They produce a result which you either use the result as a whole, or process further, but never edit by hand.

    I dislike wizards, which are passive code generators that generate a bunch of code up front, which you then modify.

    To me, this seems like automated copy-paste-modify coding.

    The other thing is that wizards sometimes hide complexity which will come back to haunt you later. The Pragmatic Programmer covers this in a section called "Evil Wizards:"
    Quote Originally Posted by The Pragmatic Programmer
    Tip 50: Don't Use Wizard Code You Don't Understand.
    As I said on the WACT mailing list:

    I've always felt that the more wizards a programming environment had, the worse it was.

    What these wizards do is try to cover up the fact that there are too many dependencies and complicated structures in the programming environment. Instead of removing the complexity, wizards just hide it, leaving it to burn you another day.

    Think about hello world in Java versus Hello world in PHP. In Java, you almost have to have a wizard just to get to the hello world running. I've been programming for 20 years and I still have trouble running naked java applications with class path configurations & enforced directory structures, etc. A simple hello world is one of PHPs greatest strengths.

    If your initial setup is so complicated as to need a wizard, the solution isn't to write a wizard, but to simplify the setup.

    Anyway, I think that code generation is an appropriate tool for a DAO solution. As long as it does not require (or even allow) you to manually edit the generated product in any way. Think active, not passive.

  22. #22
    SitePoint Zealot ZangBunny's Avatar
    Join Date
    Jul 2003
    Location
    Mainz, Germany
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ghurtado
    More to the point, the ideal code generator would be such that the code is neither specific to any single RDBMS nor DB abstraction layer.
    (...)
    Are we talking about something similar?
    No. I think the generated code should be very specific to the target RDBMS, using any special features and advantages it offers.

    We just need to make sure that the generated code provides a consistent interface, so if you originally used MySQL for a system, it should be possible to just re-generate the mapping layer for Oracle and exchange the two without changing anything else in the system.

    This way, you can take full advantage of the strong points of any database (what other reason is there for switching to another database?), while still having the flexibility to change at any time.

    Live Long and Prosper,

    Bunny

  23. #23
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone know what happened with Binarycloud? it seemed like it had a lot going for it, and then it got frozen in time, didnt it?
    No they're still going strong - see the mailing list but like Mozilla, they don't seem to be externally focused (like their website is long out of date and they're definately doing alot of something even if no one knows what it is).

  24. #24
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great to hear that they're still going at it, I suppose that at some point they will come up with something for the people to see. I can't wait...

  25. #25
    SitePoint Member
    Join Date
    Nov 2001
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've actaully been writing one for almost a year now. Unfortunately it's closed source (sorry, did it for myself), and far too plan and ugly to really show off anyways.

    I must say that it's incredibly helpful. The general process (briefly) is to create a project, add a db, add tables and fileds to that db, then generate forms for that db. The form fields have server side validation, calendar popups, file uploading and resizing (using ImageMagick) as well as saving file info to specified table. Finally add any extra components, like login (specifying which fields in what tables are used for username, password, etc).

    After all is set, I hit the generate link and it creates a full base cms with list, add, edit, delete features with all types of goodies for wach one, an includes dir with includes required (db class, calendar class, upload class, etc), an htaccess or local php.ini, and very basic lists / views for frontend. And of course the mysql sql file for importing.

    I eventaully plan to add teh ability to add design templates (sans template system as I'm not much a fan of templates in php), importing and exporting of sites (like import a sql file and generate teh goodies), basic cvs and ftp / mysql access for remote auto installation. Tons more, but i'm far too tired to remember it all right now.

    I've used the system with at least 12 sites in the past 6 months and it's cut my dev time in half since all the tedious setup time is a tiny fraction of what it could be. In other words, good luck with your system, as I HIGHLY reccommend continueing with it!

    If it's open source, I may be interested in contributing as well.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •