aboutsummaryrefslogtreecommitdiffstats
path: root/MainController.php
diff options
context:
space:
mode:
authorFilipp Lepalaan <filipp@mac.com>2010-07-20 07:22:23 +0300
committerFilipp Lepalaan <filipp@mac.com>2010-07-20 07:22:23 +0300
commitdead03e955367d4635764251f62256365c01b221 (patch)
tree24696b6896871521b12e36d778f2219f9987ac28 /MainController.php
parentad4a149c32bd42bf374b56c88cdf43b5ee801874 (diff)
downloadmain-dead03e955367d4635764251f62256365c01b221.tar.gz
main-dead03e955367d4635764251f62256365c01b221.tar.bz2
main-dead03e955367d4635764251f62256365c01b221.zip
ordering children, PHP 5.3 changes
Diffstat (limited to 'MainController.php')
-rw-r--r--MainController.php98
1 files changed, 57 insertions, 41 deletions
diff --git a/MainController.php b/MainController.php
index 4eea176..f77f1bb 100644
--- a/MainController.php
+++ b/MainController.php
@@ -16,14 +16,16 @@ class MainController
const ForeignKey = '';
const TableSelect = ''; // extra fields to select
+ public $data;
+ private $table;
+
////
// create controller object
- function __construct($id = null)
+ function __construct($where = null)
{
// 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->table = static::TableName;
// table name not defined, default to class name
@@ -33,24 +35,49 @@ class MainController
$this->mainView = new MainView();
- if ($id) {
- return $this->get($id);
+ if ($where) {
+ return $this->get($where);
}
return $this;
}
+ ////
+ // Get One Thing
+ public function get($where)
+ {
+ if (!is_array($where)) {
+ $where = array('id' => $where);
+ }
+
+ $this->find($where);
+
+ if (!is_array($this->data)) {
+ return false; // found nothing
+ }
+
+ if (count($this->data) == 1) {
+ return current($this->data);
+ }
+
+ return $this->data;
+
+ }
+
+ ////
+ // Initialize the fields of a class
+ // with the table's default values
public function init()
{
// populate indices
if ($_SESSION['config']['db.driver'] == 'mysql') {
- $schema = MainDb::fetch("DESCRIBE `{$this->table}`");
+ $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);
@@ -61,29 +88,11 @@ class MainController
}
////
- // Get One Thing
- public function get($where)
- {
- if (!is_array($where)) {
- $where = array('id' => $where);
- }
-
- $this->find($where);
-
- if (!is_array($this->data)) {
- return false; // found nothing
- }
-
- return current($this->data);
-
- }
-
- ////
// the New Find
public function find($where = null, $sort = false, $limit = false)
{
+ $q = '';
$select = '*';
- $q = '';
$this->data = array();
// allow custom queries
@@ -112,24 +121,22 @@ class MainController
}
} else {
$q = "WHERE `{$this->table}`.`id` = ?";
- $values = array($where);
+ $values = $where;
}
if ($where == NULL) {
$q = 'WHERE ?';
- $values = array(1);
+ $values = 1;
}
// $schema = App::conf('tables');
// $this->schema = $schema[$this->table];
// Ugly hack until PHP 5.3
- $i_sort = eval("return {$this->class}::OrderBy;");
- $i_fk = eval("return {$this->class}::ForeignKey;");
- $i_mtm = eval("return {$this->class}::ManyToMany;");
- $i_select = eval("return {$this->class}::TableSelect;");
-
-// $orderBy = ($sort) ? $sort :
+ $i_sort = static::OrderBy;
+ $i_fk = static::ForeignKey;
+ $i_mtm = static::ManyToMany;
+ $i_select = static::TableSelect;
if ($sort) {
list($col, $dir) = explode(' ', $sort);
@@ -154,8 +161,7 @@ class MainController
$result = MainDb::fetch($sql, $values);
if (empty($result)) {
- $this->data = false;
- return;
+ return $this->data = array();
}
for ($i=0; $i < count($result); $i++)
@@ -180,18 +186,24 @@ class MainController
private function find_children($row, $i)
{
$id = $row['id']; // ID of the parent
- $fk = explode(',', eval("return $this->class::HasMany;"));
+ $fk = explode(',', static::HasMany);
if (empty($fk[0])) {
- return false;
+ return array();
}
foreach ($fk as $child)
{
+ $order_by = '';
+
+ if ($child::OrderBy) {
+ $order_by = sprintf('ORDER BY `%s`.%s', $child, $child::OrderBy);
+ }
+
// determine nature of relationship
- $sql = "SELECT * FROM `$child` WHERE `{$this->table}_id` = ?";
$one_to_many = explode(',', eval("return $child::ForeignKey;"));
$many_to_many = explode(',', eval("return $child::ManyToMany;"));
+ $sql = "SELECT * FROM `$child` WHERE `{$this->table}_id` = ? $order_by";
if (in_array($this->table, $many_to_many)) // m/n
{
@@ -202,11 +214,15 @@ class MainController
WHERE `{$child}_{$this->table}`.`{$this->table}_id` = `$this->table`.id AND
`{$child}_{$this->table}`.`{$child}_id` = `$child`.id AND
`{$this->table}`.id = ?";
- } else if (@in_array ($table, $ref_schema['belongsTo'])) { // 1/m
+ } else if (@in_array($table, $ref_schema['belongsTo'])) { // 1/m
$sql = "SELECT * FROM `$ref` WHERE `$ref`.`{$table}_id` = ?";
}
$stmt = MainDb::query($sql, array($id));
+ if (!$stmt) {
+ return MainApp::error('Error executing query ' . $sql);
+ }
+
$this->data[$i][$child] = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
@@ -405,10 +421,10 @@ class MainController
$sql .= "`{$col}`,";
}
- $sql = rtrim($sql, ",");
+ $sql = rtrim($sql, ',');
$sql = "SELECT * FROM `{$this->table}` WHERE MATCH($sql) AGAINST('{$match}')";
- return MainApp::db()->query($sql);
+ return MainDb::fetch($sql);
}