I am writing my own MVC framework in PHP, for educational purposes only. It was actually not easy for the router / dispatcher class to invoke the correct controller / action, etc.
But now I'm in the part where I will use the models. Or actually, a model layer. But there is something bothering me.
Other MVC structures have a "BaseModel". I read that this is actually bad practice, because the "Model" should not be considered as another class. But like a real “layer” that can contain things like the “mapper” or “repository” template, etc.
But honestly, I do not see any advantages in this. For me, the BaseModel class seems to be the fastest way with the same results.
I can just do something like:
class User extends BaseModel { // the GetUserBy* could easily be something that handled by the // BaseModel class, like in the Repo pattern. public function getUserByName ( $name ) { // no error handling of any kind, just for simplicity return $this->db->exec("SELECT * FROM users WHERE name='".$name."'"); } // $data = array public function saveUser ( $data ) { // Make sure no extra fields are added to the array $user = array ( 'name' => $data['name'], 'address' => $data['address']); $this->db->autoSave ( $user ); } }
But if I went to the repository template, then I need to create the following: Repositories DAO legal entities
Entities have Aggregates with other repositories. So basically I manually write out the whole database schema for objects ...
After all, what's the difference ??? Except that I could probably save a lot of time just using the BaseModel class ...
But why is it still considered bad? It is not that the repo template separates my application more than I do now. Because for me, these patterns mentioned above seem very overpriced. It probably only works in an application that has a common state; Save objects locally (in the repository) and transfer them later.
That is why I think that no one can really answer this question ...
But I still hope to see a worthy answer that will make me go: "ahhhh ... What did I think ...". But if not, then I'm sure in my case that BaseModel is not bad at all, and that most bloggers are just a bunch of sheep :-)