Forked from Beteha/models/Model.php to standalone public library TPsoft/DBmodel,

added creator for build MODEL class,
added basic tests
This commit is contained in:
2025-05-27 23:48:20 +02:00
parent 874e2bab95
commit 4b00166f43
6 changed files with 1903 additions and 1 deletions

195
README.md
View File

@ -1,3 +1,198 @@
# DBmodel # DBmodel
This library extends the builtin PDO object by several useful features. 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);
?>
```

1453
src/DBmodel.php Normal file

File diff suppressed because it is too large Load Diff

176
src/creator.php Normal file
View File

@ -0,0 +1,176 @@
<?php
namespace TPsoft\DBmodel;
class Creator {
private $dbh;
public function __construct($dbh) {
$this->dbh = $dbh;
}
public function interact() {
$name = $this->readline('New model name: ');
$table = $this->readline('Table name: ');
$entity = $this->readline('Entity name: ');
if (strlen($entity) <= 0) {
$entity = $table;
}
$model_filepath = $this->rootDir().'/models/'.$name.'.php';
if ($this->readline("Create MODEL class '$model_filepath'? (y - yes, other - no) ") != 'y') {
echo "Creating of MODEL class is skipped\n";
return false;
}
$model_dirpath = dirname($model_filepath);
if (!file_exists($model_dirpath)) {
if ($this->readline("Create directory '$model_dirpath'? (y - yes, other - no) ") != 'y') {
echo "Creating of MODEL class is skipped\n";
return false;
}
mkdir($model_dirpath);
}
echo "Creating MODEL class ... ";
if (file_exists($model_filepath)) {
if ($this->readline("File '$model_filepath' exists. Do you want to replace? (y - yes, other - no) ") != 'y') {
echo "Creating of MODEL class is skipped\n";
return false;
}
}
$table_columns = $this->dbh->getTableColumns($table);
$suc = $this->modelSave($model_filepath, $name, $table, $entity, $table_columns['pks'], $table_columns['columns'], $table_columns['types']);
echo $suc ? "MODEL class created" : 'Error: MODEL class not created';
}
private function modelSave($filepath, $name, $tablename, $entity, $pks, $columns, $types) {
if (is_array($pks) && count($pks) > 1) {
$primary_key_name = "array('".implode("', '", $pks)."')";
$columns = array_merge($pks, $columns);
} else {
if (is_array($pks)) {
$primary_key_name = "'".$pks[0]."'";
} else {
$primary_key_name = "'$pks'";
}
}
$allowed_attributes = array();
foreach ($columns as $column) {
$allowed_attributes[] = sprintf("'%s' => '%s'", $column, str_replace("'", '"', $types[$column]));
}
if (substr($entity, -3) == 'ies') {
$entity_one = substr($entity, 0, -3).'y';
} else if (substr($entity, -2) == 'es') {
$entity_one = substr($entity, 0, -2);
} else if (substr($entity, -1) == 's') {
$entity_one = substr($entity, 0, -1);
} else {
$entity_one = $entity;
}
$auto_created_dt = in_array('created_dt', $columns)
? '
if (is_null($primary_key)
&& !isset($data[\'created_dt\']))
{
$data[\'created_dt\'] = date(\'Y-m-d H:i:s\');
}'
: '';
$auto_changed_dt = in_array('changed_dt', $columns)
? '
if (!is_null($primary_key)
&& is_array($data)
&& count($data) > 0
&& !isset($data[\'changed_dt\']))
{
$data[\'changed_dt\'] = date(\'Y-m-d H:i:s\');
}'
: '';
$content = '<?'."php
/*
TPsoft.org 2000-".date('Y')."
file for controlers/*.php
Milestones:
".date('Y-m-d H:i')." Created
*/
class ".$name." extends \TPsoft\DBmodel\DBmodel {
public $"."tables = array(
'".$entity."' => array(
'name' => '".$entity."',
'primary_key_name' => ".$primary_key_name.",
'allow_attributes' => array(
".implode(",\n\t\t\t\t", $allowed_attributes)."
)
),
);
public function exist($"."primary_key = null) {
return $"."this->existRecord('".$entity."', $"."primary_key);
}
public function ".$entity_one."($"."primary_key = null, $"."data = array()) {".$auto_created_dt.$auto_changed_dt."
return $"."this->record('".$entity."', $"."primary_key, $"."data);
}
public function ".$entity_one."By($"."colname, $"."colvalue) {
return $"."this->recordBy('".$entity."', $"."colname, $"."colvalue);
}
public function ".$entity_one."Save($"."data = array()) {
return $"."this->".$entity_one."($"."this->exist($"."data) ? $"."data : null, $"."data);
}
public function ".$entity_one."Empty() {
return $"."this->recordEmpty('".$entity."');
}
public function ".$entity_one."Attributes() {
return $"."this->typesAttributes('".$entity."');
}
public function ".$entity_one."Count() {
return $"."this->count('".$entity."');
}
public function getList($"."search = array(), $"."reverse = false, $"."concat_or = false) {
return $"."this->search('".$entity."')
->where($"."search, $"."concat_or)
->order(array(".$primary_key_name." => $"."reverse ? 'DESC' : 'ASC'))
->toArray();
}
public function ".$entity_one."Combo($"."col_key, $"."col_value, $"."add_empty = false) {
return $"."this->search('".$entity."')
->toCombo($"."col_key, $"."col_value, $"."add_empty);
}
}
?".">
";
return file_put_contents($filepath, $content);
}
/* ----------------------------------------------------
* HELPDER METHODS
*/
public function readline($question, $default = null) {
ob_flush();
$line = readline($question);
if (!is_null($default)
&& strlen($line) <= 0)
{
return $default;
}
return $line;
}
public function rootDir() {
return dirname(dirname(__FILE__));
}
}
?>

28
test/test1.php Normal file
View File

@ -0,0 +1,28 @@
<?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);
?>

12
test/test2.php Normal file
View File

@ -0,0 +1,12 @@
<?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();
?>

38
test/test3.php Normal file
View File

@ -0,0 +1,38 @@
<?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');
$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);
?>