From b09adf07febd1e3cc14449434532451c98aad2c7 Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Mon, 1 Aug 2011 15:39:42 +0300 Subject: some fixes --- MainApp.php | 9 ++++++++- MainController.php | 46 ++++++++++++++++++++++++++++++++++++++++------ MainDb.php | 3 ++- MainView.php | 5 +++++ 4 files changed, 55 insertions(+), 8 deletions(-) diff --git a/MainApp.php b/MainApp.php index 0e79f96..0cf0442 100644 --- a/MainApp.php +++ b/MainApp.php @@ -11,6 +11,7 @@ * To Public License, Version 2, as published by Sam Hocevar. See * http://sam.zoy.org/wtfpl/COPYING for more details. */ + class MainApp { //// @@ -172,7 +173,13 @@ class MainApp $file = self::conf('app.error_log'); if (!file_exists($file)) { - exit('Log file does not exist'); + // try to guess log file location + $basedir = dirname( dirname( $_SERVER['SCRIPT_FILENAME'] )); + $file = sprintf( '%s/data/logs/error.log', $basedir ); + } + + if( !file_exists( $file )) { + exit( 'Log file does not exist' ); } $fh = fopen($file, 'a+') or die('Failed to open log file'); diff --git a/MainController.php b/MainController.php index a426018..aae6d69 100644 --- a/MainController.php +++ b/MainController.php @@ -22,15 +22,16 @@ class MainController private $defaultAction = ''; // Method to run when none specified const OrderBy = ''; // which column to order the results by + const GroupBy = ''; // which column to group the results by const HasMany = ''; const TableName = ''; const ManyToMany = ''; const ForeignKey = ''; - const TableSelect = ''; // extra fields to select + const TableSelect = ''; // extra fields to select - public $data; // data returned from DB - private $table; // corresponding Db table - private $primary_key; // name of primary key column + public $data; // data returned from DB + private $table; // corresponding Db table + private $primary_key; // name of primary key column //// // create controller object @@ -106,6 +107,24 @@ class MainController } } + /** + * Return one thing, without the relatives + */ + public function one($where) + { + if (is_numeric($where)) { + $where = array('id' => $where); + } + + list($key, $value) = each($where); + + $sql = 'SELECT * FROM `%s` WHERE `%s` = ? LIMIT 1'; + $sql = sprintf($sql, $this->table, $key); + + return MainDb::one($sql, array($value)); + + } + //// // the New Find public function find($where = null, $sort = false, $limit = false) @@ -209,6 +228,7 @@ class MainController foreach ($fk as $child) { + $group_by = ''; $order_by = ''; $table = ($child::TableName) ? $child::TableName : $child; @@ -217,11 +237,24 @@ class MainController $order_by = sprintf('ORDER BY `%s`.%s', $table, $ob); } + if ($gb = $child::GroupBy) { + $group_by = sprintf('GROUP BY `%s`.%s', $table, $gb); + } + // determine nature of relationship $one_to_many = explode(',', $child::ForeignKey); $many_to_many = explode(',', $child::ManyToMany); - $sql = "SELECT * FROM `$table` WHERE `{$this->table}_id` = ? $order_by"; + $sql = 'SELECT *'; + + // allow custom selections + if ($child::TableSelect) { + $sql .= ', ' . $child::TableSelect; + } + + $sql .= " FROM `$table` WHERE `{$this->table}_id` = ? + $group_by + $order_by"; if (in_array($this->table, $many_to_many)) // m/n { @@ -236,7 +269,7 @@ class MainController 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) { @@ -315,6 +348,7 @@ class MainController //// // delete This Thing + // @return whatever MainDb returns public function delete($where, $limit = '') { if (empty($where)) { diff --git a/MainDb.php b/MainDb.php index 1f9401a..8c05aac 100644 --- a/MainDb.php +++ b/MainDb.php @@ -113,7 +113,8 @@ class MainDb return MainApp::error($error); } - } catch (PDOException $e) { + } + catch (PDOException $e) { $error = $e->getMessage() . $sql; $error .= "\n" . print_r(debug_backtrace(), TRUE); return MainApp::error($error); diff --git a/MainView.php b/MainView.php index b403466..77f083a 100644 --- a/MainView.php +++ b/MainView.php @@ -69,6 +69,11 @@ class MainView $out = '
tag('input', $params); -- cgit v1.2.3