added namespace for created new model,

added method getListByID for new model,
added type of object for import method,
extends definition tor checkDBTable,
fixed typo in parameters of method existsColumn
This commit is contained in:
Igor Miňo 2025-06-12 07:48:08 +02:00
parent 877ca38c0b
commit 616dc01c21
3 changed files with 19 additions and 8 deletions

View File

@ -23,10 +23,11 @@ class Creator {
$name = $this->readline('New model name: ');
$table = $this->readline('Table name: ');
$entity = $this->readline('Entity name: ');
$namespace = $this->readline('Namespace [App\Models]: ', 'App\Models');
if (strlen($entity) <= 0) {
$entity = $table;
}
$model_filepath = $this->rootDir().'/models/'.$name.'.php';
$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;
@ -47,11 +48,11 @@ class Creator {
}
}
$table_columns = $this->dbh->getTableColumns($table);
$suc = $this->modelSave($model_filepath, $name, $table, $entity, $table_columns['pks'], $table_columns['columns'], $table_columns['types']);
$suc = $this->modelSave($model_filepath, $name, $table, $entity, $table_columns['pks'], $table_columns['columns'], $table_columns['types'], $namespace);
echo $suc ? "MODEL class created" : 'Error: MODEL class not created';
}
private function modelSave($filepath, $name, $tablename, $entity, $pks, $columns, $types) {
private function modelSave($filepath, $name, $tablename, $entity, $pks, $columns, $types, $namespace) {
if (is_array($pks) && count($pks) > 1) {
$primary_key_name = "array('".implode("', '", $pks)."')";
$columns = array_merge($pks, $columns);
@ -93,6 +94,7 @@ class Creator {
$data[\'changed_dt\'] = date(\'Y-m-d H:i:s\');
}'
: '';
$namespace_str = strlen($namespace) > 0 ? "\nnamespace $namespace;\n" : '';
$content = '<?'."php
/*
TPsoft.org 2000-".date('Y')."
@ -101,7 +103,7 @@ class Creator {
Milestones:
".date('Y-m-d H:i')." Created
*/
".$namespace_str."
class ".$name." extends \TPsoft\DBmodel\DBmodel {
public $"."tables = array(
@ -149,6 +151,15 @@ class ".$name." extends \TPsoft\DBmodel\DBmodel {
->toArray();
}
public function getListByID($"."search = array(), $"."reverse = false, $"."concat_or = false) {
$"."all = $"."this->getList($"."search, $"."reverse, $"."concat_or);
$"."ret = array();
if (is_array($"."all)) foreach ($"."all as $"."key => $"."row) {
$"."ret[$"."row[$primary_key_name]] = $"."row;
}
return $"."ret;
}
public function ".$entity_one."Combo($"."col_key, $"."col_value, $"."add_empty = false) {
return $"."this->search('".$entity."')
->toCombo($"."col_key, $"."col_value, $"."add_empty);

View File

@ -851,7 +851,7 @@ class DBmodel
return $this->allowNextError();
}
public function import($objModel)
public function import(DBmodel $objModel)
{
if (is_null($objModel)) return false;
if (

View File

@ -87,7 +87,7 @@ class Maintenance
);
}
public function checkDBTable($table_name, $definition)
public function checkDBTable($table_name, $definition, $after_definition = '')
{
if (strlen($table_name) <= 0) {
$this->lastMessage = 'Table ' . strtoupper($table_name) . ' - invalid index';
@ -99,7 +99,7 @@ class Maintenance
// TEST output
$table_name,
// FIX query
'CREATE TABLE `' . $table_name . '` (' . $definition . ')'
'CREATE TABLE `' . $table_name . '` (' . $definition . ') ' . $after_definition
);
$this->lastMessage = 'Table ' . strtoupper($table_name) . ' (' . $table_name . ')';
return $status;
@ -146,7 +146,7 @@ class Maintenance
public function checkDBRetype($table_name, $column, $new_type, $definition)
{
if (!$this->existsColumn($table_name, $column_old)) {
if (!$this->existsColumn($table_name, $column)) {
return Maintenance::ABNORMAL;
}
$status = $this->checkDB(