Re: design pattern/code generators

From: Date: Sun, 30 Jan 2005 15:21:01 +0000
Subject: Re: design pattern/code generators
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-207714@lists.php.net to get a copy of this message
robert mena wrote:
Hi All, I am looking for advice regarding design patterns/code generation in PHP5. I have a simple code generation tool (written in PHP) to interface with database. It works fine for simple situations but seems a little "strange" for more complex ones. Suppose I have a table user (id, name, age) and a table account(idAccount, idUser, name). My generator creates a class for each table and a standard db class. Each class basically has a set/get method for each property and some "standard" insert, delete, update, search methods ex. class user { var $db function setName(..) function getName() function insert() {
      $this->db->query("insert into user values...")
} ... } I've omited the other methods/constructor but you can get the picture. The problem comes when I have to access information the comes from 2+ tables. Suppose I have to show all users accounts. Now I end up with something like this. $u = new User() ; $a = new Account() ; if($u->search()) { for($i=0;$i<...) {
      $idUser = $u->getId() ;  $u->next() ;
       $a->setIdUser($idUser) ;
       if($a->search())
{
      // print
} } While this works I feel there must be an easier/cleaner way. If I was just querying the database a join would give me the result in one pass.
the join is based on a foreign key constraint - An idea might be to generate a method based on the FK details (in either or both relevant classes), if you are using an older version of mysql, or don't actually define the constraint in the DB, then this maybe impossible - unless you use a config file to generate each class. so that you can do: $user = new User(); $ac = $user->getAccount(); // returns the correct Account object.
In my case what should I do ? create a new method "showAccounts" ? Where should I put it, in User or Account class ?
on the User class, Account would probably want a method like showUser().
I am interested in advices regarding how to design it better so I can make the proper adjustments in my generator.


« previous php.general (#207714) next »