/** * getColumnData * * @param $table * * @return array */ public static function getColumnData($table) { $db = Database::getInstance(); $sql = "SHOW FULL COLUMNS FROM `{$table}`"; $statement = $db->prepare($sql); $statement->execute(); $columns = $statement->fetchAll(\PDO::FETCH_ASSOC); return $columns; }
public function __construct() { if (isset($_SESSION['admin-role'])) { $db = Database::getInstance(); $sql = "\n\t\t\t\tSELECT\n\t\t\t\t\tt1.title\n\t\t\t\tFROM role_permissions t2\n\t\t\t\t JOIN user_permissions t1 ON t2.permission_id = t1.id\n\t\t\t\tWHERE t2.role_id = ?\n\t\t\t"; $statement = $db->prepare($sql); $statement->execute([$_SESSION['admin-role']]); foreach ($statement->fetchAll(\PDO::FETCH_ASSOC) as $permission) { $this->permissions[$permission['title']] = true; } } }
public function save() { $db = Database::getInstance(); $data = $this->_data; $key = $this->_idColumn; if (isset($this->_data['created_at']) && $this->_data['created_at'] == '0000-00-00 00:00:00') { $this->_data['created_at'] = date('Y-m-d H:i:s', time()); } if (isset($this->_data['updated_at'])) { $this->_data['updated_at'] = date('Y-m-d H:i:s', time()); } // saving as a new record if (empty($this->_data[$this->_idColumn])) { if (isset($this->_data['created_at'])) { $this->_data['created_at'] = date('Y-m-d H:i:s', time()); } if (isset($this->_data['updated_at'])) { $this->_data['updated_at'] = date('Y-m-d H:i:s', time()); } $columns = array_keys($this->_data); $values = array_values($this->_data); $columnsInsert = implode(", ", $columns); $valuesInsert = implode(", ", array_pad([], count($values), '?')); $sql = "INSERT INTO {$this->_table} ({$columnsInsert}) VALUES ({$valuesInsert});"; } else { // or saving an update // for an update we remove the primary key // and use it at the end for the where clause if (isset($this->_data['updated_at'])) { $this->_data['updated_at'] = date('Y-m-d H:i:s', time()); } $columns = array_diff_key($this->_data, [$key => $key]); $values = array_values($columns); $updates = []; foreach ($columns as $k => $v) { $updates[] = "{$k} = ?"; } $updates = implode(', ', $updates); $sql = "UPDATE {$this->_table} SET {$updates} WHERE {$key} = {$data[$key]}"; } $statement = $db->prepare($sql); return $statement->execute($values); }
public static function delete($id) { $db = Database::getInstance(); $table = Gravel::$config['database']['table_prefix'] . static::$table; $idColumn = static::$idColumn; $sql = "DELETE FROM {$table} WHERE {$idColumn} = ?;"; $statement = $db->prepare($sql); return $statement->execute([$id]); }