patternphpMinor
A class which represents an SQL table
Viewed 0 times
sqlwhichclassrepresentstable
Problem
I'm representing DB tables as classes (in this case PHP classes). This has the goal of modeling every section of the web page, where you will get inputs from the user.
My actual base design is:
-
One class per table and every row is represented by an Object of this class.
-
The constructors asks for what they need in order to get the table row which they will represent:
info = $this->getRowFromVideos($id);
}
}
The way I access the DB is not important here.
Every field of the table is stored as an array in an
For composed tables like Players who needs an User and a Game ID, the query would be like this:
And the constructor class asks for an
Of course, these classes are not designed to satisfy CRUD needs, like inserting a new user or deleting a video. Those responsibilities would be encapsulated in other classes.
I cannot see which problems may appear with this design in the future if the web page gets really big.
My actual base design is:
-
One class per table and every row is represented by an Object of this class.
-
The constructors asks for what they need in order to get the table row which they will represent:
info = $this->getRowFromVideos($id);
}
}
The way I access the DB is not important here.
Every field of the table is stored as an array in an
$info variable. And I have a method to access for the whole row or just a field.function getInfo($key = false){
if($key) return $this->info[$key];
else return $this->info;
}For composed tables like Players who needs an User and a Game ID, the query would be like this:
SELECT * FROM players WHERE userID = 'xxxxxx' AND gameID = 'SC2'And the constructor class asks for an
User object and Game object:class Player extends DB {
private $info = array();
private $user;
private $game;
public function __construct($user, $game){
$this->user = $user;
$this->game = $game;
$this->info =
$this->getRowFromPlayers($user->getInfo("userID"),$game->getInfo("gameID"));
}
}Of course, these classes are not designed to satisfy CRUD needs, like inserting a new user or deleting a video. Those responsibilities would be encapsulated in other classes.
I cannot see which problems may appear with this design in the future if the web page gets really big.
Solution
Take a look at
Object Relational Mapping,
especially
Object-relational impedance mismatch
on Wikipedia.
Basically there problems with this as soon as it gets a bit more
complicated and you need to query e.g. a lot of objects at once, or you
have joins, and so on.
Let's take
will fetch one row from the database. So you already have a
bottleneck here because you'll have to run
each of your
their IDs, which is very inefficient. So you'll have to construct
objects from the outside, mapping a result of multiple rows from the
database to newly constructed (or cached) objects.
Another problem often comes up in that your objects don't exactly are
the table rows, they are just mapped onto objects, so testing by
identity is mostly meaningless unless you keep track of objects and
return the same object for the same row of a table. It gets complicated
very fast.
Obviously there are already solutions for this, so I suggest you take a
look at existing PHP libraries for this.
Object Relational Mapping,
especially
Object-relational impedance mismatch
on Wikipedia.
Basically there problems with this as soon as it gets a bit more
complicated and you need to query e.g. a lot of objects at once, or you
have joins, and so on.
Let's take
$this->getRowFromPlayers. I'm assuming, that that methodwill fetch one row from the database. So you already have a
bottleneck here because you'll have to run
n separate queries foreach of your
n IDs when you want to get like a list of objects bytheir IDs, which is very inefficient. So you'll have to construct
objects from the outside, mapping a result of multiple rows from the
database to newly constructed (or cached) objects.
Another problem often comes up in that your objects don't exactly are
the table rows, they are just mapped onto objects, so testing by
identity is mostly meaningless unless you keep track of objects and
return the same object for the same row of a table. It gets complicated
very fast.
Obviously there are already solutions for this, so I suggest you take a
look at existing PHP libraries for this.
Context
StackExchange Code Review Q#71795, answer score: 2
Revisions (0)
No revisions yet.