Witam,


Znalazlem tutorial active record na stronce ale mam pytanie bo jednak cos jest nie tak.

Oto kod ktory znalazlem :

  1. class User{
  2.  
  3. private $firstName;
  4.  
  5. private $lastName;
  6.  
  7. private $email;
  8.  
  9. private $table='users';
  10.  
  11. public function __construct(){}
  12.  
  13. // set first name
  14.  
  15. public function setFirstName($firstName){
  16.  
  17. $this->firstName=$firstName;
  18.  
  19. }
  20.  
  21. // set last name
  22.  
  23. public function setLastName($lastName){
  24.  
  25. $this->lastName=$lastName;
  26.  
  27. }
  28.  
  29. // set email
  30.  
  31. public function setEmail($email){
  32.  
  33. $this->email=$email;
  34.  
  35. }
  36.  
  37. // fetch row
  38.  
  39. public function fetch($id){
  40.  
  41. if(!$row=mysql_query("SELECT * FROM $this->table WHERE id='$id'")){
  42.  
  43. throw new Exception('Error fetching row');
  44.  
  45. }
  46.  
  47. return $row;
  48.  
  49. }
  50.  
  51. // insert row
  52.  
  53. public function insert(){
  54.  
  55. if(!mysql_query("INSERT INTO $this->table (firstname,lastname,email) VALUES ($this->firstName,$this->lastName,$this->email)")){
  56.  
  57. throw new Exception('Error inserting row');
  58.  
  59. }
  60.  
  61. }
  62.  
  63. // update row
  64.  
  65. public function update($id){
  66.  
  67. if(!mysql_query("UPDATE $this->table SET firstname='$this->firstName,lastname=$this->lastName,email=$this->email WHERE id='$id'")){
  68.  
  69. throw new Exception('Error updating row');
  70.  
  71. }
  72.  
  73. }
  74.  
  75. // delete row
  76.  
  77. public function delete($id){
  78.  
  79. if(!mysql_query("DELETE FROM $this->table WHERE id='$id'")){
  80.  
  81. throw new Exception('Error deleting row');
  82.  
  83. }
  84.  
  85. }
  86.  
  87. }
  88.  
  89.  





No wiec tutaj jak widac pomieszana jest klasa active record (user) z zapytaniami sql, tak mi przynajmniej powiedzial moj kolega programista ze ta klasa dlatego jest do bani, wiec moje pytanie brzmi jak to naprawic ?

Czy wystarczy stworzyc nowa klase ktora w metodach bedzie posiadac tylko i wylacznie zapytania sql a nastepnie zalaczyc te klase do napisanej powyzej za pomoca require i wykorzystac jej funkcje ?

Czy dobrze rozumuje ?