design pattern/code generators
| From: | robert mena | Date: | Sun, 30 Jan 2005 01:17:12 +0000 |
| Subject: | design pattern/code generators | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-207701@lists.php.net to get a copy of this message | ||
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.
In my case what should I do ? create a new method "showAccounts" ?
Where should I put it, in User or Account class ?
I am interested in advices regarding how to design it better so I can
make the proper adjustments in my generator.