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

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__));
}
}
?>