logger = new Logger('database_' . date('Ymd')); } public function databaseLog() { return $this->logger; } protected function strquery($stmt, $params) { foreach ($params as $key => $value) { if (is_null($value) || (is_string($value) && $value == '')) { $value = null; } if (is_bool($value)) { $value = (int)$value; } $stmt->bindValue(":{$key}", $value, (is_int($value) ? PDO::PARAM_INT : PDO::PARAM_STR)); } } function create($query, $data) { try { $stmt = Connection::getInstance()->prepare($query); $this->strquery($stmt, $data); $stmt->execute(); return Connection::getInstance()->lastInsertId(); } catch (PDOException $e) { $this->logger->error(print_r(['error' => $e->getMessage(), 'query' => $query, 'data' => $data], true)); } } function read($query, $data = null) { try { $stmt = Connection::getInstance()->prepare($query); $this->strquery($stmt, $data); $stmt->execute(); if ($stmt->rowCount() > 0) { return $stmt; } return null; } catch (PDOException $e) { $this->logger->error(print_r(['error' => $e->getMessage(), 'query' => $query, 'data' => $data], true)); } } function delete($query, $data): int { try { $stmt = Connection::getInstance()->prepare($query); $this->strquery($stmt, $data); $stmt->execute(); return ($stmt->rowCount() ? 1 : 0); } catch (Exception $e) { $this->logger->error(print_r(['error' => $e->getMessage(), 'query' => $query, 'data' => $data], true)); } return 0; } function update($query, $data): int { try { $stmt = Connection::getInstance()->prepare($query); $this->strquery($stmt, $data); $stmt->execute(); return $stmt->rowCount() ? 1 : 0; } catch (Exception $e) { $this->logger->error(print_r(['error' => $e->getMessage(), 'query' => $query, 'data' => $data], true)); } return 0; } }