3 abstract class DatabaseTable {
4 protected $idcolumn = 'id';
11 public function __construct($table, $id=false, $column_or_db=false, $column=false)
15 $this->table = $table;
17 if ($column_or_db && is_object($column_or_db)) {
18 $this->db = $column_or_db;
25 if ($id && !$col) $this->load($id);
26 if ($id && $col) $this->loadByColumn($id,$col);
28 $path = substr($_SERVER['HTTP_REFERER'], strpos($_SERVER['HTTP_REFERER'], $_SERVER['SERVER_NAME'])+strlen($_SERVER['SERVER_NAME'])+1);
32 while (($pos = strpos($path, '/', $pos)) !== false) {
33 $this->rootPath .= '../';
38 protected function postLoad() {}
40 protected function load($id)
42 $sql = sprintf("SELECT * FROM %s WHERE %s = %d LIMIT 1",
43 $this->table, $this->idcolumn, $id);
44 $this->data = $this->db->fetchObject($sql);
46 $idcolumn = $this->idcolumn;
47 $this->id = $this->data->$idcolumn;
54 protected function loadByColumn($id,$column)
56 $sql = sprintf("SELECT * FROM %s WHERE %s = %s",
57 $this->table, $column, $this->db->quote($id));
58 $this->data = $this->db->fetchObject($sql);
60 $idcolumn = $this->idcolumn;
61 $this->id = $this->data->$idcolumn;
71 public function fetch()
76 public function modify($column, $value=false, $id=false)
78 if (!is_array($column) && $value !== false)
79 $column = array($column => $value);
80 elseif (!is_array($column) || $value !== false)
81 throw Exception('modify called with wrong parameters');
83 if ($this->db->update($this->table, $column, $this->idcolumn.'='.($id === false ? $this->id : $id)))
84 return $id === false ? $this->load($this->id) : true;
89 public function delete($id=false)
91 $this->db->delete($this->table, $this->idcolumn, $id === false ? $this->id : $id);
94 public function getAttribute($attr)
96 if ($this->data === false) return NULL;
97 if (property_exists($this->data, $attr))
98 return $this->data->$attr;
103 public function quote($string) { return $this->db->quote($string); }
104 public function execute($sql) { return $this->db->execute($sql); }
105 public function fetchValue($sql) { return $this->db->fetchValue($sql); }
106 public function fetchAssoc($sql) { return $this->db->fetchAssoc($sql); }
107 public function fetchAssocList($sql) { return $this->db->fetchAssocList($sql); }
108 public function fetchObject($sql) { return $this->db->fetchObject($sql); }
109 public function fetchObjectList($sql) { return $this->db->fetchObjectList($sql); }