db = S_db2::getInstance(); $this->db->loadModule('Date', 'Date'); $this->db->loadModule('Extended', 'Extended'); $table = 'tbl'.$table; $this->table = $this->db->$table; return true; } function _initFields() { if (!is_array($this->fields)){ $qr = 'SHOW FIELDS FROM '.$this->table; $this->fields = $this->db->queryCol($qr); } return true; } function getFieldsValues($data) { $this->_initFields(); if (!isset($data['changed'])) $data['changed'] = $this->db->Date->mdbNow(); foreach ($data as $field => $value) { if (!in_array($field, $this->fields)) { unset($data[$field]); } } return $data; } function insert($data) { if (!isset($data['added'])) $data['added'] = $this->db->Date->mdbNow(); $data = $this->getFieldsValues($data); $res = $this->db->Extended->autoExecute($this->table, $data, MDB2_AUTOQUERY_INSERT); if (PEAR::isError($res)) { if (DEBUG) { vdie('DBTable->insert error', $res->getUserInfo(), $data, debug_backtrace()); } else { sendDeveloperEmail($res->getUserInfo(), $data); dump($res->getUserInfo(), 'DBTable->insert error'); } } return $this->db->lastInsertID($this->table); } function update($data, $where) { $data = $this->getFieldsValues($data); $res = $this->db->Extended->autoExecute($this->table, $data, MDB2_AUTOQUERY_UPDATE, $where); if (PEAR::isError($res)) { if (DEBUG) { vdie('DBTable->update error', $res->getUserInfo(), $data, debug_backtrace()); } else { sendDeveloperEmail($res->getUserInfo(), $data); dump($res->getUserInfo(), 'DBTable->update error'); } } return $res; } function delete($where) { $res = $this->db->Extended->autoExecute($this->table, null, MDB2_AUTOQUERY_DELETE, $where); if (PEAR::isError($res)) { dump($res->getUserInfo(), 'DBTable->update error'); } return $res; } }