I am developing my own class model for my database and I would like to know what you guys think of it.
For each table in my database I have 2 classes. The first one, is a fetching class (I don't know how to call it..), this class has some static methods interacting with the database (fetchAll, fetchById etc..). The second class is the model itself, it contains all methods for a row of the table (insert, update, delete, validate or any other processing on the row).
Each of the fetching class returns the model object, if it's a fetchAll, for example, the method will return an array of model, and if its a fetchById the method will return only 1 object, since id is unique.
Example of how I would use it :
$users = Users_Map::fetchAll();
foreach($users as $user)
{
echo $user->username.'<br/>';
}
$user = new User_Model();
$user->username = 'example';
$user->insert();
$user = User_Map::fetchById(1);
$user->username = 'example2';
$user->update();
If you have any suggestion other than _Map
, I'm open.
I'd like to know if this “pattern” is good and what do you think of it, how can I improve it, am I wrong? Should I use only 1 class, having static and non-static method all in the _Model
?
Thank you very much for your help/suggestions!