aboutsummaryrefslogtreecommitdiffstats
path: root/MainController.php
diff options
context:
space:
mode:
authorFilipp Lepalaan <filipp@mac.com>2010-07-15 23:51:05 +0300
committerFilipp Lepalaan <filipp@mac.com>2010-07-15 23:51:05 +0300
commitad4a149c32bd42bf374b56c88cdf43b5ee801874 (patch)
tree98dfdd80228531b9c25b23a57607954f0aa715d1 /MainController.php
parentb4cd2ad59440171ea330ddaad28b4743d4936b51 (diff)
downloadmain-ad4a149c32bd42bf374b56c88cdf43b5ee801874.tar.gz
main-ad4a149c32bd42bf374b56c88cdf43b5ee801874.tar.bz2
main-ad4a149c32bd42bf374b56c88cdf43b5ee801874.zip
sqlite support
Diffstat (limited to 'MainController.php')
-rw-r--r--MainController.php53
1 files changed, 40 insertions, 13 deletions
diff --git a/MainController.php b/MainController.php
index 81c68bf..4eea176 100644
--- a/MainController.php
+++ b/MainController.php
@@ -20,21 +20,18 @@ class MainController
// create controller object
function __construct($id = null)
{
- // child classes should always have the same name as their tables
+ // child classes typically have the same name as their tables
+ // but not always
$this->class = get_class($this);
- $this->table = eval("return {$this->class}::TableName;");
- $this->mainView = new MainView();
+// $this->table = eval("return {$this->class}::TableName;");
+ $this->table = static::TableName;
// table name not defined, default to class name
if (!$this->table) {
$this->table = strtolower($this->class);
}
-
- // populate indices
- $schema = MainDb::fetch("DESCRIBE `{$this->table}`");
- foreach ($schema as $s) {
- $this->data[$s['Field']] = $s['Default'];
- }
+
+ $this->mainView = new MainView();
if ($id) {
return $this->get($id);
@@ -44,6 +41,25 @@ class MainController
}
+ public function init()
+ {
+ // populate indices
+ if ($_SESSION['config']['db.driver'] == 'mysql') {
+ $schema = MainDb::fetch("DESCRIBE `{$this->table}`");
+ foreach ($schema as $s) {
+ $this->data[$s['Field']] = $s['Default'];
+ }
+ return $schema;
+ }
+ if ($_SESSION['config']['db.driver'] == 'sqlite') {
+ $sql = 'PRAGMA TABLE_INFO('.$this->table.')';
+ $schema = MainDb::fetch($sql);
+ foreach ($schema as $s) {
+ $this->data[$s['name']] = '';
+ }
+ }
+ }
+
////
// Get One Thing
public function get($where)
@@ -146,8 +162,12 @@ class MainController
{
$row = $result[$i];
$this->data[$i] = $row;
- $this->find_parent($row, $i);
- $this->find_children($row, $i);
+ if (static::ForeignKey) {
+ $this->find_parent($row, $i);
+ }
+ if (static::HasMany) {
+ $this->find_children($row, $i);
+ }
}
return $this->data;
@@ -248,8 +268,11 @@ class MainController
$insert = '';
$values = array();
- foreach($data as $k => $v)
- {
+ if (@empty($data['id'])) {
+ unset($data['id']);
+ }
+
+ foreach($data as $k => $v) {
$insert .= "`{$k}`, ";
$values[":{$k}"] = $v;
}
@@ -270,6 +293,10 @@ class MainController
return MainApp::error('Cannot delete without arguments');
}
+ if (!is_array($where)) {
+ $where = array('id' => $where);
+ }
+
list($key, $value) = each($where);
if ($limit) {