4.1 KiB
4.1 KiB
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);
?>