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; } function isUnique($field, $value) { $sql = 'SELECT 1 ' . 'FROM `' . $this->table . '` ' . 'WHERE ' . $this->db->quoteIdentifier($field) . ' = ' . $this->db->quote($value) . ' ' . 'LIMIT 1'; $res = $this->db->queryOne($sql); Qs_Db::isError($res); return null === $res; } protected function _getColumnsString(array $columns) { if (empty($columns)) { return '*'; } $parts = array(); foreach ($columns as $column) { $parts[] = $this->db->quoteIdentifier($column); } return implode(', ', $parts); } function find($id, $columns = array()) { $sql = 'SELECT ' . $this->_getColumnsString($columns) . ' ' . 'FROM `' . $this->table . '` ' . 'WHERE `id` = ' . $this->db->quote($id, 'integer') . ' ' . 'LIMIT 1'; $res = $this->db->queryRow($sql); Qs_Db::isError($res); return null === $res ? false : $res; } function findField($id, $field, $default = null) { if (false === ($row = $this->find($id, $field))) { return $default; } return Qs_Array::get($row, $field, $default); } }