3 abstract class DatabaseTable {
4 protected $idcolumn = 'id';
10 public function __construct($table, $id=false, $column_or_db=false, $column=false)
14 $this->table = $table;
16 if ($column_or_db && is_object($column_or_db)) {
17 $this->db = $column_or_db;
24 if ($id && !$col) $this->load($id);
25 if ($id && $col) $this->loadByColumn($id,$col);
28 protected function postLoad() {}
30 protected function load($id)
32 $sql = sprintf("SELECT * FROM %s WHERE %s = %d LIMIT 1",
33 $this->table, $this->idcolumn, $id);
34 $this->data = $this->db->fetchObject($sql);
36 $idcolumn = $this->idcolumn;
37 $this->id = $this->data->$idcolumn;
44 protected function loadByColumn($id,$column)
46 $sql = sprintf("SELECT * FROM %s WHERE %s = %s",
47 $this->table, $column, $this->db->quote($id));
48 $this->data = $this->db->fetchObject($sql);
50 $idcolumn = $this->idcolumn;
51 $this->id = $this->data->$idcolumn;
61 public function fetch()
66 public function modify($column, $value=false, $id=false)
68 if (!is_array($column) && $value !== false)
69 $column = array($column => $value);
70 elseif (!is_array($column) || $value !== false)
71 throw Exception('modify called with wrong parameters');
73 if ($this->db->update($this->table, $column, $this->idcolumn.'='.($id === false ? $this->id : $id)))
74 return $id === false ? $this->load($this->id) : true;
79 public function delete($id=false)
81 $this->db->delete($this->table, $this->idcolumn, $id === false ? $this->id : $id);