From a14477fd1140d09c3079b3477be265cc5e1410da Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Tue, 17 Nov 2009 09:49:44 +0200 Subject: More fixes --- App.php | 6 +++++- Controller.php | 33 ++++++++++++++++++++++++++++++--- Db.php | 3 ++- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/App.php b/App.php index 1c45735..3085efe 100644 --- a/App.php +++ b/App.php @@ -175,8 +175,12 @@ class App * @param string [$where] URL to redirect to * @return void */ - static function redirect($url) + static function redirect($url = null) { + if (!$url) { + // Is it smart to redirect back to the page which redirected here? + $url = $_SERVER['HTTP_REFERER']; + } header("HTTP/1.1 303 See Other"); header("Location: $url"); } diff --git a/Controller.php b/Controller.php index 5e14f4b..14b7900 100644 --- a/Controller.php +++ b/Controller.php @@ -24,15 +24,20 @@ class Controller // Child classes should always have the same name as their tables $this->class = get_class($this); $this->table = eval("return {$this->class}::TableName;"); + + $this->view = new MainView(); + + // Table name not defined, default to class name if (!$this->table) { $this->table = strtolower($this->class); } + $this->result = null; + if ($id) { return $this->get($id); } - $this->view = new MainView(); return $this; } @@ -40,9 +45,15 @@ class Controller /** * Get One Thing */ - public function get($id) + public function get($where) { - return $this->find(array('id' => $id)); + if (!is_array($where)) { + $where = array('id' => $where); + } + + $this->find($where); + return current($this->data); + } public function db() @@ -292,6 +303,7 @@ class Controller } $query = ""; $values = array(); + $data = array_merge($data, $where); foreach ($data as $k => $v) { $query .= "`$k` = :$k, "; @@ -373,6 +385,21 @@ class Controller return App::db()->query($sql); } + + /** + * Insert or update + */ + public function upsert($data, $where = null) + { + if(!$this->get($where)) { + $out = $this->insert($data); + } else { + $out = $this->update($data, $where); + } + + return $out; + + } } diff --git a/Db.php b/Db.php index d57af10..f09cd85 100644 --- a/Db.php +++ b/Db.php @@ -109,7 +109,8 @@ class Db */ public static function fetch($sql, $data = null) { - $stmt = self::query($sql, $data); + $stmt = self::query($sql, $data) + or exit(App::error("Error executing query $sql")); return $stmt->fetchAll(PDO::FETCH_ASSOC); } -- cgit v1.2.3