PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/CRUD.php

https://gitlab.com/emad.rashad/mls-grading
PHP | 140 lines | 114 code | 11 blank | 15 comment | 9 complexity | 3ed0bb45d7dde54c6c8b16e8dfed6a0f MD5 | raw file
  1. <?php
  2. require_once("MysqlDatabase.php");
  3. class CRUD {
  4. //get all rows from table
  5. public static function find_all($sort_filed = null, $order_by = null){
  6. $sql = "SELECT * FROM ".static::$table_name;
  7. if(!empty($sort_filed)){
  8. $sql .= " ORDER BY ".$sort_filed." ".$order_by;
  9. }
  10. return static::find_by_sql($sql);
  11. }
  12. //get all custom filed
  13. public static function find_all_by_custom_filed($filed, $value, $sort_filed = null, $order_by = null ){
  14. global $database;
  15. $value = $database->escape_values($value);
  16. $sql = "SELECT * FROM ".static::$table_name." WHERE {$filed} = '{$value}' ";
  17. if(!empty($sort_filed) && !empty($order_by)){
  18. $sql .= " ORDER BY ".$sort_filed." ".$order_by;
  19. }
  20. return static::find_by_sql($sql);
  21. }
  22. //get by id
  23. public static function find_by_custom_filed($filed, $value){
  24. global $database;
  25. $value = $database->escape_values($value);
  26. $sql = "SELECT * FROM ".static::$table_name." WHERE {$filed} = '{$value}' ";
  27. $result_array = static::find_by_sql($sql);
  28. return !empty($result_array)? array_shift($result_array) : false;
  29. }
  30. //get by id
  31. public static function find_by_id($id){
  32. $sql = "SELECT * FROM ".static::$table_name." WHERE id = {$id} ";
  33. $result_array = static::find_by_sql($sql);
  34. return !empty($result_array)? array_shift($result_array) : false;
  35. }
  36. //excute sql command and loop throw rows then fetch array
  37. protected static function find_by_sql($sql){
  38. global $database;
  39. $result_set = $database->query($sql);
  40. $object_array = array();
  41. while($row = $database->fetch_array($result_set)){
  42. $object_array[] = static::instantiate($row);
  43. }
  44. return $object_array;
  45. }
  46. //Object Initialization
  47. protected static function instantiate($record){
  48. $called_class = get_called_class();
  49. $object = new $called_class;
  50. foreach($record as $attribute => $value){
  51. if($object->has_attribute($attribute)){
  52. $object->$attribute = $value;
  53. }
  54. }
  55. return $object;
  56. }
  57. //check if attribute exist in class
  58. protected function has_attribute($attribute){
  59. $object_vars = $this->attribute();
  60. return array_key_exists($attribute, $object_vars);
  61. }
  62. //get only filed wich defined as attribute exist in db
  63. //(macth attribute which exist in $database_fields with class attributes)
  64. protected function attribute(){
  65. //this scope for parent class
  66. //instatiate parent class
  67. $attribute = array();
  68. foreach(static::$primary_fields as $field){
  69. if(property_exists($this, $field)){
  70. $attribute[$field] = $this->$field;
  71. }
  72. }
  73. return $attribute;
  74. }
  75. //return cleaned attributes
  76. protected function sanitized_attribute(){
  77. global $database;
  78. $clean_attributes = array();
  79. foreach($this->attribute() as $key => $value){
  80. $clean_attributes[$key] = $database->escape_values($value);
  81. }
  82. return $clean_attributes;
  83. }
  84. //update command
  85. public function update(){
  86. global $database;
  87. $attributes = $this->sanitized_attribute();
  88. $attributes_pairs = array();
  89. foreach($attributes as $key => $value){
  90. $attributes_pairs[] = "{$key}='{$value}'";
  91. }
  92. $sql = "UPDATE ".static::$table_name." SET ";
  93. $sql .= join(",", $attributes_pairs);
  94. $sql .= " WHERE id = ".$this->id;
  95. if( $database->query($sql)){
  96. return true;
  97. }else{
  98. return false;
  99. }
  100. }
  101. //insert command
  102. public function insert(){
  103. global $database;
  104. $attributes = $this->sanitized_attribute();
  105. $sql = "INSERT INTO ".static::$table_name." (";
  106. $sql .= join(',', array_keys($attributes));
  107. $sql .= ")VALUES('";
  108. $sql .= join("','", array_values($attributes));
  109. $sql .= "')";
  110. if($database->query($sql)){
  111. $this->id = $database->inserted_id();
  112. return true;
  113. }else{
  114. return false;
  115. }
  116. }
  117. //delete command
  118. public function delete(){
  119. global $database;
  120. $sql = "DELETE FROM ".static::$table_name." WHERE id = ".$this->id;
  121. $database->query($sql);
  122. if($database->mysql_affected_rows() == 1){
  123. return true;
  124. }else{
  125. return false;
  126. }
  127. }
  128. }
  129. ?>