aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--App.php6
-rw-r--r--Controller.php33
-rw-r--r--Db.php3
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);
}