Im currently developing a MVC application Framework and I have come for some advise in regards to the way I should construct my Model Layers.
The model is constructed so that each model is mapped to a table within the database for that applications, so a typical application would have
- Configuration
- Topics
- Forums
and each would be mapped to there named PHP file such as app/models/configuration.php
Now the problem I am having is creating the parent database class to be able handle specific table data, for example:
class PDOModel
{
public function __construct()
{
$this->__Communicator = Registry::getPDOInstance();
}
public function getSingle($id)
{
return /*Row*/;
}
/*Etc*/
}
And then something like this for the application model’s
class Model_Topic extends PDOModel
{
protected $__id_column = 'id';
}
and then within my controller I can use like so:
$Topic = $this->model->topic->get(22);
But i would also like to take into considerations auto joining tables as well, are there any simple lightweight libraries about that have been tested and fit the my requirements.
Any help would be highly appreciated.
Your base model class shouldn’t inherit from the database access class. Rather, it should use the database class (or mapper, who says it must always be a database?) and then provide the ORM methods (get, insert, update, etc…).
As others have posted, you should use one of the many fine pre-built solutions. As far as looking for a lightweight ORM, others have asked this before. Here’s a link to one of those questions:
https://stackoverflow.com/questions/1995834/looking-a-lightweight-php-orm