From 78112fa3ee94f7f29f842f82fac992c9e6bd236f Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Tue, 27 Oct 2009 09:19:29 +0200 Subject: More fixes --- App.php | 3 ++- Controller.php | 28 +++++++++------------------- Db.php | 6 +++--- 3 files changed, 14 insertions(+), 23 deletions(-) diff --git a/App.php b/App.php index 9912b31..bacabbf 100644 --- a/App.php +++ b/App.php @@ -138,8 +138,9 @@ class App return false; } + $msg = trim($msg); $fh = fopen($file, "a+"); - fwrite($fh, $msg); + fwrite($fh, "$msg\n"); fclose($fh); } diff --git a/Controller.php b/Controller.php index d5813a1..e25c575 100644 --- a/Controller.php +++ b/Controller.php @@ -107,7 +107,7 @@ class Controller $sql .= " LIMIT $limit"; } - $result = DB::query($sql, $values)->fetchAll(PDO::FETCH_ASSOC); + $result = DB::fetch($sql, $values); for ($i=0; $i < count($result); $i++) { @@ -216,14 +216,9 @@ class Controller * Insert this thing in the DB and return inserted * thing */ - public function insert($data = null) + public function insert() { - if (!$data) { - $data = $_POST; - } - if (empty($data)) { - App::log("Attempted to insert empty data"); return App::error("Nothing to insert"); } @@ -247,17 +242,16 @@ class Controller /** * Delete this thing */ - public function delete() + protected function delete($where) { - if (empty($_POST)) { - exit(App::error("Delete without arguments")); + if (empty($where)) { + return App::error("Delete without arguments"); } - list($key, $value) = each($_POST); + list($key, $value) = each($where); $sql = "DELETE FROM `{$this->table}` WHERE `{$key}` = ?"; - - return DB::query($sql, $value); + return Db::query($sql, array($value)); } @@ -266,13 +260,9 @@ class Controller * We keep this in the Controller since it might know * more about the topmost class */ - public function update($data = null, $where = null) + public function update($data, $where = null) { - if (!$data) { - $data = $_POST; - } - - if (empty($data)) { + if (!is_array($data)) { return App::error("Update with empty parameters"); } diff --git a/Db.php b/Db.php index e453955..730fcdf 100644 --- a/Db.php +++ b/Db.php @@ -54,7 +54,7 @@ class Db * Execute an SQL query * @return mixed */ - public function query($sql, $data = null) + public static function query($sql, $data = null) { if (!$data) { $data = array(); @@ -104,9 +104,9 @@ class Db } - public function fetch($sql, $data = null) + public static function fetch($sql, $data = null) { - $stmt = DB::query($sql, $data); + $stmt = self::query($sql, $data); return $stmt->fetchAll(PDO::FETCH_ASSOC); } -- cgit v1.2.3 From 4a7a8168743588ac37b81fbfe5e27677a67749ce Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Wed, 28 Oct 2009 21:30:39 +0200 Subject: Commit --- App.php | 1 + Controller.php | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/App.php b/App.php index bacabbf..42f8b4c 100644 --- a/App.php +++ b/App.php @@ -142,6 +142,7 @@ class App $fh = fopen($file, "a+"); fwrite($fh, "$msg\n"); fclose($fh); + } /** diff --git a/Controller.php b/Controller.php index e25c575..432ae93 100644 --- a/Controller.php +++ b/Controller.php @@ -216,7 +216,7 @@ class Controller * Insert this thing in the DB and return inserted * thing */ - public function insert() + public function insert($data) { if (empty($data)) { return App::error("Nothing to insert"); @@ -249,9 +249,10 @@ class Controller } list($key, $value) = each($where); + $data = array(":{$key}" => $value); + $sql = "DELETE FROM `{$this->table}` WHERE `{$key}` = :{$key}"; - $sql = "DELETE FROM `{$this->table}` WHERE `{$key}` = ?"; - return Db::query($sql, array($value)); + return Db::query($sql, $data); } @@ -260,7 +261,7 @@ class Controller * We keep this in the Controller since it might know * more about the topmost class */ - public function update($data, $where = null) + protected function update($data, $where = null) { if (!is_array($data)) { return App::error("Update with empty parameters"); -- cgit v1.2.3