Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a single crud model for all controllers instead of separate models for each controller

Tags:

oop

php

crud

so i've seen ususally a model is representation of a table in database like for users table it goes something like :

class user_model {

  public $id ; 
  public $username ; 
  public $password ; 
  public $email ;


  public function save(){
     $db->query(" insert into `users` (username , email , password ) values ( '$this->username' , '$this->email' , '$this->password' ) ");
  }

  public function delete(  ){
     $db->query(" delete  from users where id = $this->id   ");
  }

}

but this proccess is pretty slow and what most of models do is basic CRUD operation ... so i use a single crud model for almost all of my controllers like :

class crud_model {

  public function save( $tbl ,  $data ){
     $db->query(" insert into $tbl  ( ".explode( ',' , array_keys($data) )." ) values ( ".explode( ',' , $data )." ) ");
  }

  public function delete(  $tbl ,  $data ){
     $db->query(" delete  from $tbl  where $data['column'] = $data['val'] ");
  }

}

pleas note this is a very simplified version of my model and basically its nothing like the original code ( im using active record in the original code and it can handle complex scenarios ) so ignore syntax and technical errors

so i want to know if there is a problem with this approach ? am i missing something ?

whats the point of having lots of models when you can get by with one CRUD model .... it just seems like wast of time

like image 563
max Avatar asked Oct 30 '22 12:10

max


1 Answers

Your approach is not necessarily wrong. As programmers, we like things to be consistent so that when you have to make modifications to your code you don't need to worry about one table model working differently from another table model. We're also lazy (in a good way) so that you want to be able to write your CRUD class once and use it everywhere. You've got that part down with your single class idea.

However, if you take your CRUD class and then have all of your table models inherit from it you get the benefits of having written the code once, plus if you need to do something with a table that falls outside of your base CRUD code you can easily override or add to the functionality in your child table class.

In terms of the pattern of having a model to represent your tables, with your current approach, you can't truly represent individual tables with a generic CRUD class. Model classes will often have additional class properties that are specific to the table being represented. This allows you to design your classes so that if they were to be re-used, the developer has a better understanding of the underlying data structure. Look at your example of the User class in your original question. That class has properties such as $id, $username and $password. A developer can look at that and know exactly what is needed to create a new User.

like image 95
ski4404 Avatar answered Nov 15 '22 06:11

ski4404