added method getListByID for new model, added type of object for import method, extends definition tor checkDBTable, fixed typo in parameters of method existsColumn
DBmodel
This library extends the builtin PDO object by several useful features.
Basic usage
For testing create database test
with user test
and password test
. In database create table test
and insert same testing data.
CREATE TABLE `test` (
`id` int(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`created` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
For basic testing run this code
<?php
require_once __DIR__.'/../src/DBmodel.php';
$db = new \TPsoft\DBmodel\DBmodel('mysql:host=127.0.0.1;dbname=test;charset=utf8mb4', 'test', 'test');
// test query
$result = $db->query("show tables");
print_r($result);
// test results
$result = $db->getAll("show databases");
print_r($result);
$result = $db->getAll("show tables");
print_r($result);
$result = $db->getCol("show tables");
print_r($result);
$result = $db->getTableColumns("test");
print_r($result);
$result = $db->getRow("select * from test where id = 10");
print_r($result);
?>
Interactive create MODEL class
Create file creatorModel.php
and paste bellow code
<?php
require_once __DIR__.'/../src/DBmodel.php';
require_once __DIR__.'/../src/creator.php';
$db = new \TPsoft\DBmodel\DBmodel('mysql:host=127.0.0.1;dbname=test;charset=utf8mb4', 'test', 'test');
$creator = new \TPsoft\DBmodel\Creator($db);
$creator->interact();
?>
now for each table run php createModel.php
for interactive creating class for table.
$ php test2.php
New model name: Test
Table name: test
Entity name: test
Create MODEL class '/your/project/models/Test.php'? (y - yes, other - no) y
Create directory '/your/project/models'? (y - yes, other - no) y
Creating MODEL class ... MODEL class created
Creator build this MODEL class file
<?php
/*
TPsoft.org 2000-2025
file for controlers/*.php
Milestones:
2025-05-27 21:36 Created
*/
class Test extends \TPsoft\DBmodel\DBmodel {
public $tables = array(
'test' => array(
'name' => 'test',
'primary_key_name' => 'id',
'allow_attributes' => array(
'name' => 'varchar(255)',
'created' => 'datetime'
)
),
);
public function exist($primary_key = null) {
return $this->existRecord('test', $primary_key);
}
public function test($primary_key = null, $data = array()) {
return $this->record('test', $primary_key, $data);
}
public function testBy($colname, $colvalue) {
return $this->recordBy('test', $colname, $colvalue);
}
public function testSave($data = array()) {
return $this->test($this->exist($data) ? $data : null, $data);
}
public function testEmpty() {
return $this->recordEmpty('test');
}
public function testAttributes() {
return $this->typesAttributes('test');
}
public function testCount() {
return $this->count('test');
}
public function getList($search = array(), $reverse = false, $concat_or = false) {
return $this->search('test')
->where($search, $concat_or)
->order(array('id' => $reverse ? 'DESC' : 'ASC'))
->toArray();
}
public function testCombo($col_key, $col_value, $add_empty = false) {
return $this->search('test')
->toCombo($col_key, $col_value, $add_empty);
}
}
?>
Using MODEL class in your project
Illustrative example
<?php
require_once __DIR__.'/../src/DBmodel.php';
require_once __DIR__.'/../models/test.php';
$db = new \TPsoft\DBmodel\DBmodel('mysql:host=127.0.0.1;dbname=test;charset=utf8mb4', 'test', 'test');
// empty contructor of new MODEL class use last database handler
$test = new Test();
// exists ID 11
$result = $test->exist(11);
print_r($result);
// insert new record
$result = $test->test(null, array('name' => 'new record'));
print_r($result);
// update record ID 11
$result = $test->test(11, array('name' => 'updated record'));
print_r($result);
// get record ID 11
$result = $test->test(11);
print_r($result);
// delete record ID 11
$result = $test->test(11, null);
print_r($result);
// get all records
$result = $test->getList();
print_r($result);
// get combined list of ID and name
$result = $test->testCombo('id', 'name');
print_r($result);
?>
Maintenance of database schema
When developing a project, a situation arises where the DB schema needs to be modified and distributed to all installations. To ease these problems, the Maintenance class was created, which contains several useful methods. These first check if the schema contains the requested change and if it doesn't, they apply a FIX. Here is a short demonstration of what we can fix automatically in the project when changing the DB schema.
<?php
require_once __DIR__.'/../src/DBmodel.php';
require_once __DIR__.'/../src/Maintenance.php';
$db = new \TPsoft\DBmodel\DBmodel('mysql:host=127.0.0.1;dbname=test;charset=utf8mb4', 'test', 'test');
$maintenance = new \TPsoft\DBmodel\Maintenance($db);
// Check if existing table, if not create it by definition
$suc = $maintenance->checkDBTable('users', '
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
');
echo "\nCheckDBTable: "; var_dump($suc);
// Check if existing column, if not add it by definition
$suc = $maintenance->checkDBAdd('users', 'password', 'varchar(255) DEFAULT NULL');
echo "\nCheckDBAdd: "; var_dump($suc);
// Check if existing column
$suc = $maintenance->existsColumn('users', 'email');
echo "\nExistsColumn: "; var_dump($suc);
// Check if existing column in new name, if not rename it
$suc = $maintenance->checkDBRename('users', 'email', 'email_new', 'varchar(255) DEFAULT NULL');
echo "\nCheckDBRename: "; var_dump($suc);
// Chceck type of column, if not matched change it
$suc = $maintenance->checkDBRetype('users', 'email_new', 'varchar(128)', 'varchar(128) DEFAULT NULL AFTER `password`');
echo "\nCheckDBRetype: "; var_dump($suc);
// Remove column from table
$suc = $maintenance->checkDBRemove('users', 'email_new');
echo "\nCheckDBRemove: "; var_dump($suc);
// Add index for column
$suc = $maintenance->addKey('users', 'name');
echo "\nAddKey: "; var_dump($suc);
// Drop index by column name
$suc = $maintenance->dropKey('users', 'name');
echo "\nDropKey: "; var_dump($suc);
echo "\nDone\n\n";
?>