Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions class.db.php → DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,26 @@
* ### Usage
*
* <code>
* \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();
* </code>
*
* ### 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 <hi.i.am.jaime@gmail.com>
* @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.
Expand Down
66 changes: 32 additions & 34 deletions class.record.php → Record.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php
namespace Module\DB;

require_once(dirname(__FILE__) . '/class.db.php');
require_once(dirname(__FILE__) . '/../../core/class.debug.php');
use Sleepy\Core\Hook;
use Sleepy\Core\Module;
use Sleepy\Core\Debug;
use Module\DB\Connection;

/**
* Base class that represents a record in a table.
Expand All @@ -15,33 +17,33 @@
* ### Usage
*
* <code>
* // 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';
* </code>
*
* You can then save the new information by calling the save method:
*
* <code>
* $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();
* </code>
*
* You can also show a nice form to edit or add new records like this
*
* <code>
* $u->form(array(
* 'first_name' => 'First Name: ',
* 'last_name' => 'Last Name: ',
* 'phone' => '(800) 555-5555'
* 'name' => 'Full Name: ',
* 'email' => 'Email Address: '
* ));
* </code>
*
* ### Changelog
*
* ## Version 2.0
* * Made 2.x compatible
*
* ## Version 1.3
* * Bugfix when columns are reserved names
Expand All @@ -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 <hi.i.am.jaime@gmail.com>
* @version 1.3
* @version 2.0
* @license http://opensource.org/licenses/MIT
**/

Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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()) {
Expand All @@ -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.");
}
}
Expand All @@ -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;
Expand Down Expand Up @@ -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);

?>
<li>
Expand Down Expand Up @@ -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,
Expand Down
32 changes: 15 additions & 17 deletions class.recordlist.php → RecordList.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
<?php
namespace Module\DB;

require_once(dirname(__FILE__) . '/class.record.php');
require_once(dirname(__FILE__) . '/../../core/class.hooks.php');
use Sleepy\Core\Hook;
use Module\DB\Record;

/**
* Create an iterable list of records
*
* ### Usage
*
* <code>
* $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();
Expand All @@ -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 <jaime@rodriguez-jr.com>
* @version 1.0
* @version 2.0
* @license http://opensource.org/licenses/MIT
**/

Expand All @@ -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}");
Expand All @@ -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);
}

Expand Down
3 changes: 3 additions & 0 deletions db_test.php → db_test.php.old
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

// @todo make this work with PHPUnit

require_once('class.db.php');
require_once('class.record.php');

Expand Down