From df627ab016de4e3b118e0865393ce2fbca8d9ffc Mon Sep 17 00:00:00 2001 From: Jaime Rodriguez Date: Thu, 30 Jul 2020 22:26:51 -0700 Subject: [PATCH] update: 2.x compatibility --- class.db.php => DB.php | 20 ++++---- class.record.php => Record.php | 66 +++++++++++++------------- class.recordlist.php => RecordList.php | 32 ++++++------- db_test.php => db_test.php.old | 3 ++ 4 files changed, 61 insertions(+), 60 deletions(-) rename class.db.php => DB.php (80%) rename class.record.php => Record.php (87%) rename class.recordlist.php => RecordList.php (73%) rename db_test.php => db_test.php.old (98%) diff --git a/class.db.php b/DB.php similarity index 80% rename from class.db.php rename to DB.php index 41b2e42..bd79d48 100644 --- a/class.db.php +++ b/DB.php @@ -10,24 +10,26 @@ * ### Usage * * - * \Module\DB\DB::$dbhost = 'localhost'; - * \Module\DB\DB::$dbname = 'db'; - * \Module\DB\DB::$dbuser = 'username'; - * \Module\DB\DB::$dbpass = 'itsmeopenup'; - * $db = \Module\DB\DB::getInstance(); + * \Module\DB\Connection::$dbhost = 'localhost'; + * \Module\DB\Connection::$dbname = 'db'; + * \Module\DB\Connection::$dbuser = 'username'; + * \Module\DB\Connection::$dbpass = 'itsmeopenup'; + * $db = \Module\DB\Connection::getInstance(); * * * ### Changelog - * + * ## Version 2.0 + * * Made 2.x compatible + * * ## Version 1.12 * * Added the date section to the documentation * - * @date August 13, 2014 + * @date July 30, 2020 * @author Jaime A. Rodriguez - * @version 1.2 + * @version 2.0 * @license http://opensource.org/licenses/MIT */ -abstract class DB { +abstract class Connection { /** * PDO The single instance is stored here. diff --git a/class.record.php b/Record.php similarity index 87% rename from class.record.php rename to Record.php index e06b798..3af679d 100644 --- a/class.record.php +++ b/Record.php @@ -1,8 +1,10 @@ - * // load a record with id= 5 from a table called 'user' - * class user extends \Module\DB\Record { - * public $table = 'user'; + * use Module\DB\Record + * + * class User extends Record { + * public $table = 'users'; * } * - * $u = new user(); - * $u->load(5); - * $u->columns['first_name'] = 'Joe'; - * - * - * You can then save the new information by calling the save method: - * - * - * $u->save(); + * $u = new User(); + * $u->columns['user_id'] = 0; + * $u->columns['name'] = "John Doe"; + * $u->columns['email'] = "john@doe.com"; + * $u->columns['password'] = password_hash('asdf', PASSWORD_DEFAULT); + * $u->save(); * * * You can also show a nice form to edit or add new records like this * * * $u->form(array( - * 'first_name' => 'First Name: ', - * 'last_name' => 'Last Name: ', - * 'phone' => '(800) 555-5555' + * 'name' => 'Full Name: ', + * 'email' => 'Email Address: ' * )); * * * ### Changelog + * + * ## Version 2.0 + * * Made 2.x compatible * * ## Version 1.3 * * Bugfix when columns are reserved names @@ -53,13 +55,9 @@ * ## Version 1.1 * * Added the date section to the documentation * - * @section dependencies Dependencies - * * class.hooks.php - * * class.db.php - * - * @date May 15, 2019 + * @date July 30, 2020 * @author Jaime A. Rodriguez - * @version 1.3 + * @version 2.0 * @license http://opensource.org/licenses/MIT **/ @@ -95,7 +93,7 @@ class Record { * @param string $id The id to load automatically */ public function __construct($id=0) { - $this->db = DB::getInstance(); + $this->db = Connection::getInstance(); $select = $this->db->query("SELECT * FROM `{$this->table}` LIMIT 1"); @@ -173,9 +171,9 @@ public function save() { $result = $this->db->prepare($sql); // save - \Sleepy\Hook::addAction('recordBeforeSave'); + Hook::addAction('recordBeforeSave'); $result->execute($col); - \Sleepy\Hook::addAction('recordAfterSave'); + Hook::addAction('recordAfterSave'); if ($new) { if ($result->rowCount()) { @@ -195,14 +193,14 @@ public function save() { * @return bool True if delete is successful */ public function delete() { - \Sleepy\Hook::addAction('recordBeforeDelete'); + Hook::addAction('recordBeforeDelete'); $query = $this->db->prepare('DELETE FROM ' . $this->table . " WHERE {$this->primaryKey}=:{$this->primaryKey}"); - //\Sleepy\Debug::out($query->debugDumpParams()); - if ($query->execute(array(":{$this->primaryKey}" => $this->columns[$this->primaryKey]))) { - \Sleepy\Hook::addAction('recordDeleteSucessful'); + + if ($query->execute(array(":{$this->primaryKey}" => $this->columns[$this->primaryKey]))) { + Hook::addAction('recordDeleteSucessful'); return true; } else { - \Sleepy\Hook::addAction('recordDeleteError'); + Hook::addAction('recordDeleteError'); throw new \Exception("{$this->table}: Record was not deleted."); } } @@ -215,7 +213,7 @@ public function delete() { * @param boolean $submit Show the submit button? * @return void returns nothing */ - public function form(Array $fields, $legend='table_name', $submit=true) { + public function form(Array $fields=[], $legend='table_name', $submit=true) { // Legend defaults to the table name if ($legend == 'table_name') { $legend = $this->table; @@ -259,7 +257,7 @@ public function form(Array $fields, $legend='table_name', $submit=true) { } } - $label = \Sleepy\Hook::addFilter('dbForm_label', $label); + $label = Hook::addFilter('dbForm_label', $label); ?>
  • @@ -347,7 +345,7 @@ public function form(Array $fields, $legend='table_name', $submit=true) { 'dbForm_list', $this->table . "_dbForm_list" ) as $filterName) { - $buffer = \Sleepy\Hook::addFilter($filterName, Array( + $buffer = Hook::addFilter($filterName, Array( $buffer, $meta['native_type'], $label, diff --git a/class.recordlist.php b/RecordList.php similarity index 73% rename from class.recordlist.php rename to RecordList.php index f21367e..63846e2 100644 --- a/class.recordlist.php +++ b/RecordList.php @@ -1,8 +1,8 @@ - * $facilities = new \Module\DB\RecordList(new Facility(), 'active=1'); + * use Module\DB\RecordList; + * use Example\Record\Facility; + * + * $facilities = new RecordList(new Facility(), 'active=1'); * foreach ($facilities as $f) { * $f->columns['active'] = 0; * $f->save(); @@ -19,17 +22,12 @@ * * ### Changelog * - * ## Version 1.0 - * * + * ## Version 2.0 + * * Made 2.x compatible * - * @section dependencies Dependencies - * * class.hooks.php - * * class.db.php - * * class.record.php - * - * @date May 10, 2019 + * @date July 30, 2020 * @author Jaime A. Rodriguez - * @version 1.0 + * @version 2.0 * @license http://opensource.org/licenses/MIT **/ @@ -44,11 +42,11 @@ public function __construct(Record $record, string $where = "", array $data = [] $items = []; - $this->key = \Sleepy\Hook::addFilter('record_list_key', $record->primaryKey); - $this->table = \Sleepy\Hook::addFilter('record_list_table', $record->table); - $this->where = \Sleepy\Hook::addFilter('record_list_where', $where); + $this->key = Hook::addFilter('record_list_key', $record->primaryKey); + $this->table = Hook::addFilter('record_list_table', $record->table); + $this->where = Hook::addFilter('record_list_where', $where); - $this->db = DB::getInstance(); + $this->db = Connection::getInstance(); if (!empty($this->where)) { $query = $this->db->prepare("SELECT `{$this->key}` FROM `{$this->table}` WHERE {$this->where}"); @@ -67,7 +65,7 @@ public function __construct(Record $record, string $where = "", array $data = [] foreach ($rows = $query->fetchAll() as $row) { $instance = clone $record; $instance->load($row[$this->key]); - \Sleepy\Hook::addFilter("record_list_item", $instance); + Hook::addFilter("record_list_item", $instance); array_push($items, $instance); } diff --git a/db_test.php b/db_test.php.old similarity index 98% rename from db_test.php rename to db_test.php.old index 4663311..18a6877 100644 --- a/db_test.php +++ b/db_test.php.old @@ -1,4 +1,7 @@