DatabaseObject.php (3774B)
1 <?php 2 3 namespace Lollipop { 4 require_once "SQLDatabase.php"; 5 6 abstract class DatabaseObject 7 { 8 protected string $table; 9 protected string $primary; 10 11 protected SQLDatabase $db; 12 protected array $data = []; 13 protected array $changed_keys = []; 14 15 function __construct(SQLDatabase $db) 16 { 17 $this->db = $db; 18 $this->primary = $this->get_primary(); 19 $this->table = $this->get_table(); 20 } 21 22 abstract static function get_primary(): string; 23 abstract static function get_table(): string; 24 25 public function setData($data) 26 { 27 $this->data = $data; 28 } 29 public function where(string $key, string $value) 30 { 31 $sql = "SELECT * FROM {$this->table} WHERE $key = ?"; 32 $value = array($value); 33 $stmt = $this->db->conn->prepare($sql); 34 $stmt->execute($value); 35 $result = $stmt->get_result(); 36 if ($result->num_rows == 0) { 37 return false; 38 } 39 $this->data = $result->fetch_assoc(); 40 return true; 41 } 42 43 public function where_array(array $values) : bool 44 { 45 $sql = "SELECT * FROM {$this->table} WHERE "; 46 $params = []; 47 $i = 0; 48 foreach($values as $key => $param){ 49 if($i > 0) 50 $sql .= " and "; 51 $sql .= "{$key} = ?"; 52 $params[] = $param; 53 } 54 55 $stmt = $this->db->conn->prepare($sql); 56 $stmt->execute($params); 57 $result = $stmt->get_result(); 58 59 if ($result->num_rows == 0) { 60 return false; 61 } 62 63 $this->data = $result->fetch_assoc(); 64 return true; 65 } 66 public function load(string $id): bool 67 { 68 /*this fuction accepts an $id value for the primary key 69 * loads the row into data[] 70 * returns bool if row is found 71 */ 72 $sql = "SELECT * FROM {$this->table} WHERE {$this->primary} = ?"; 73 74 $stmt = $this->db->conn->prepare($sql); 75 $stmt->execute([$id]); 76 $result = $stmt->get_result(); 77 78 if ($result->num_rows == 0) { 79 return false; 80 } 81 82 $this->data = $result->fetch_assoc(); 83 return true; 84 } 85 86 public function save() : bool 87 { 88 if (!$this->changed_keys) 89 return false; 90 91 $sql = "UPDATE {$this->table} SET "; 92 93 $values = []; 94 foreach ($this->changed_keys as $index => $key) { 95 if ($index > 0) 96 $sql .= ', '; 97 $sql .= "$key = ?"; 98 $values[] = $this->data[$key]; 99 } 100 101 $sql .= " WHERE {$this->primary} = ?"; 102 $values[] = $this->data[$this->primary]; 103 104 $stmt = $this->db->conn->prepare($sql); 105 106 $this->changed_keys = []; 107 108 if($stmt->execute($values)) 109 return true; 110 else 111 return false; 112 } 113 114 public function add() : bool 115 /* this function add the set variables to the database */ 116 { 117 if (!$this->changed_keys) 118 return false; 119 120 $sql = "INSERT INTO {$this->table} ("; 121 $sql_val = ") VALUES ("; 122 $values = []; 123 124 foreach ($this->changed_keys as $index => $key) { 125 if ($index > 0){ 126 $sql .= ', '; 127 $sql_val .= ', '; 128 } 129 $sql .= $key; 130 $sql_val .= "?"; 131 $values[] = $this->data[$key]; 132 } 133 134 $sql .= $sql_val . ")"; 135 $stmt = $this->db->conn->prepare($sql); 136 137 $this->changed_keys = []; 138 139 if($stmt->execute($values)) 140 return true; 141 else 142 return false; 143 } 144 public function delete() 145 { 146 $sql = "DELETE FROM {$this->table} WHERE {$this->primary} = ?"; 147 $stmt = $this->db->conn->prepare($sql); 148 $stmt->execute([$this->data[$this->primary]]); 149 $this->data = []; 150 $this->changed_keys = []; 151 } 152 153 public function __get(string $name) 154 { 155 return $this->data[$name]; 156 } 157 158 public function __set(string $name, $value) 159 { 160 $this->data[$name] = $value; 161 $this->changed_keys[] = $name; 162 } 163 164 public function getData() 165 { 166 return $this->data; 167 } 168 } 169 }