diff --git a/src/ex/models/app/home_model.php b/src/ex/models/app/home_model.php index 6fb1e22..7ef44be 100644 --- a/src/ex/models/app/home_model.php +++ b/src/ex/models/app/home_model.php @@ -4,75 +4,65 @@ declare(strict_types=1); namespace prj\ex\models\app; -class home_model { - private $dbh; - private $name_demo_table = "test_names"; - private $db_helper; +class home_model extends \tts\database\model { + protected $pdo; + protected $table = "test_names"; public function __construct($dbh) { // \tts\exceptions\DB_Exception::$error_message = "Home Model Error"; - $this->dbh = $dbh; - $this->db_helper = new \tts\database\help_save($this->dbh, $this->name_demo_table); - } - - public function is_valid(array $data): bool { - return \tts\database\help_load::is_valid($data); + $this->pdo = $dbh; } public function pre_save(): bool { - $missing = $this->db_helper->get_missing(); + $missing = $this->get_missing(); if (count($missing)) { echo "Required fields not set " . implode(",", $missing) . "!"; return false; } - if ($this->db_helper->dump_diff()) { + if ($this->dump_diff()) { return false; // Opps forgot a field! } return true; } public function init_name_demo_table(): void { - $create_table_sql = "CREATE TABLE IF NOT EXISTS `{$this->name_demo_table}`( + $create_table_sql = "CREATE TABLE IF NOT EXISTS `{$this->table}`( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `first_name` TEXT, `last_name` TEXT, `age` INTEGER )"; try { - $this->dbh->query($create_table_sql); + $this->pdo->query($create_table_sql); } catch (\PDOException $e) { echo \tts\exceptions\DB_Exception::customMessage($e, "Unable to make table!"); } } public function populate($rows): void { - $dummy = new \tts\database\dummy_data($this->dbh); + $dummy = new \tts\database\dummy_data($this->pdo); $data = [ 'first_name' => \tts\arrays\mocking\rnd_names::get_first_names(), 'last_name' => \tts\arrays\mocking\rnd_names::get_last_names(), 'age' => \tts\arrays\mocking\rnd_names::mock_age() ]; try { - $dummy->add_dummy_data($this->name_demo_table, $rows, $data); + $dummy->add_dummy_data($this->table, $rows, $data); } catch (\PDOException $e) { echo \tts\exceptions\DB_Exception::customMessage($e, "Unable to populate table!"); } } - - public function get_member(string $member): ?string { - return $this->db_helper->get_member($member); - } - + public function save_new_user(array $input): bool { - $set = $this->db_helper->set_members_by_generator($input); + $set = $this->set_members_by_generator($input); if ($set === false) { return false; // Found Validation issues in input } try { - $status = $this->db_helper->save($this); + $status = $this->save(); } catch (\PDOException $e) { echo \tts\exceptions\DB_Exception::customMessage($e, "Unable to save changes!"); } - if ($status === $this->db_helper::successful_save) { + if ($status === self::successful_save) { return true; // It saved } return false; // It did not save @@ -80,12 +70,12 @@ class home_model { public function get_users(int $number_of_users_to_get = 12): \Generator { if ($number_of_users_to_get === 0) { - return \tts\database\help_load::empty_generator(); + return self::empty_generator(); } try { - $sql = "SELECT * FROM {$this->name_demo_table} LIMIT {$number_of_users_to_get}"; - $result = $this->dbh->query($sql); - return \tts\database\help_load::pdo_fetch_lazy($result); + $sql = "SELECT * FROM {$this->table} LIMIT {$number_of_users_to_get}"; + $result = $this->pdo->query($sql); + return self::pdo_fetch_lazy($result); } catch (\PDOException $e) { echo \tts\exceptions\DB_Exception::customMessage($e); } diff --git a/src/mockup/go_text_templates/models.txt b/src/mockup/go_text_templates/models.txt index 0877710..26ae5ab 100644 --- a/src/mockup/go_text_templates/models.txt +++ b/src/mockup/go_text_templates/models.txt @@ -4,57 +4,55 @@ declare(strict_types=1); namespace prj\{{.Root}}\models\{{.Subfolder}}; -class {{.File}}_model { - private $dbh; - private $demo_table = "test_names"; - private $db_helper; +class {{.File}}_model extends \tts\database\model { + protected $pdo; + protected $table = "test_names"; - public function __construct($dbh) { - $this->dbh = $dbh; - $this->db_helper = new \tts\database\help_save($this->dbh, $this->demo_table); + public function __construct($pdo) { + $this->pdo = $pdo; } public function pre_save(): bool { - $missing = $this->db_helper->get_missing(); + $missing = $this->get_missing(); if (count($missing)) { echo "Required fields not set " . implode(",", $missing) . "!"; return false; } - if ($this->db_helper->dump_diff()) { + if ($this->dump_diff()) { return false; // Opps forgot a field! } return true; } public function init_demo_table(): void { - $create_table_sql = "CREATE TABLE IF NOT EXISTS `{$this->demo_table}`( + $create_table_sql = "CREATE TABLE IF NOT EXISTS `{$this->table}`( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `first_name` TEXT, `last_name` TEXT )"; - $this->dbh->query($create_table_sql); + $this->pdo->query($create_table_sql); } public function populate($rows): void { - $dummy = new \tts\database\dummy_data($this->dbh); + $dummy = new \tts\database\dummy_data($this->pdo); $data = [ 'first_name' => \tts\arrays\mocking\rnd_names::get_first_names(), 'last_name' => \tts\arrays\mocking\rnd_names::get_last_names(), ]; - $dummy->add_dummy_data($this->demo_table, $rows, $data); + $dummy->add_dummy_data($this->table, $rows, $data); } public function save_{{.Method}}(array $input): bool { - $set = $this->db_helper->set_members_by_generator($input); + $set = $this->set_members_by_generator($input); if ($set === false) { return false; // Found Validation issues in input } try { - $status = $this->db_helper->save($this); + $status = $this->save(); } catch (\PDOException $e) { echo \tts\exceptions\DB_Exception::customMessage($e, "Unable to save changes!"); } - if ($status === $this->db_helper::successful_save) { + if ($status === self::successful_save) { return true; // It saved } return false; // It did not save @@ -62,12 +60,12 @@ class {{.File}}_model { public function get_{{.Method}}(int $number_of_users_to_get = 12): \Generator { if ($number_of_users_to_get === 0) { - return \tts\database\help_load::empty_generator(); + return self::empty_generator(); } try { - $sql = "SELECT * FROM {$this->demo_table} LIMIT {$number_of_users_to_get}"; - $result = $this->dbh->query($sql); - return \tts\database\help_load::pdo_fetch_lazy($result); + $sql = "SELECT * FROM {$this->table} LIMIT {$number_of_users_to_get}"; + $result = $this->pdo->query($sql); + return self::pdo_fetch_lazy($result); } catch (\PDOException $e) { echo \tts\exceptions\DB_Exception::customMessage($e); } diff --git a/src/mockup/models/app/home_model.php b/src/mockup/models/app/home_model.php index 3f305f5..c09e2f6 100644 --- a/src/mockup/models/app/home_model.php +++ b/src/mockup/models/app/home_model.php @@ -4,75 +4,65 @@ declare(strict_types=1); namespace prj\mockup\models\app; -class home_model { - private $dbh; - private $name_demo_table = "test_names"; - private $db_helper; +class home_model extends \tts\database\model { + protected $pdo; + protected $table = "test_names"; public function __construct($dbh) { // \tts\exceptions\DB_Exception::$error_message = "Home Model Error"; - $this->dbh = $dbh; - $this->db_helper = new \tts\database\help_save($this->dbh, $this->name_demo_table); + $this->pdo = $dbh; } - - public function is_valid(array $data): bool { - return \tts\database\help_load::is_valid($data); - } - + public function pre_save(): bool { - $missing = $this->db_helper->get_missing(); + $missing = $this->get_missing(); if (count($missing)) { echo "Required fields not set " . implode(",", $missing) . "!"; return false; } - if ($this->db_helper->dump_diff()) { + if ($this->dump_diff()) { return false; // Opps forgot a field! } return true; } public function init_name_demo_table(): void { - $create_table_sql = "CREATE TABLE IF NOT EXISTS `{$this->name_demo_table}`( + $create_table_sql = "CREATE TABLE IF NOT EXISTS `{$this->table}`( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `first_name` TEXT, `last_name` TEXT, `age` INTEGER )"; try { - $this->dbh->query($create_table_sql); + $this->pdo->query($create_table_sql); } catch (\PDOException $e) { echo \tts\exceptions\DB_Exception::customMessage($e, "Unable to make table!"); } } public function populate($rows): void { - $dummy = new \tts\database\dummy_data($this->dbh); + $dummy = new \tts\database\dummy_data($this->pdo); $data = [ 'first_name' => \tts\arrays\mocking\rnd_names::get_first_names(), 'last_name' => \tts\arrays\mocking\rnd_names::get_last_names(), 'age' => \tts\arrays\mocking\rnd_names::mock_age() ]; try { - $dummy->add_dummy_data($this->name_demo_table, $rows, $data); + $dummy->add_dummy_data($this->table, $rows, $data); } catch (\PDOException $e) { echo \tts\exceptions\DB_Exception::customMessage($e, "Unable to populate table!"); } } - - public function get_member(string $member): ?string { - return $this->db_helper->get_member($member); - } public function save_new_user(array $input): bool { - $set = $this->db_helper->set_members_by_generator($input); + $set = $this->set_members_by_generator($input); if ($set === false) { return false; // Found Validation issues in input } try { - $status = $this->db_helper->save($this); + $status = $this->save(); } catch (\PDOException $e) { echo \tts\exceptions\DB_Exception::customMessage($e, "Unable to save changes!"); } - if ($status === $this->db_helper::successful_save) { + if ($status === self::successful_save) { return true; // It saved } return false; // It did not save @@ -80,12 +70,12 @@ class home_model { public function get_users(int $number_of_users_to_get = 12): \Generator { if ($number_of_users_to_get === 0) { - return \tts\database\help_load::empty_generator(); + return self::empty_generator(); } try { - $sql = "SELECT * FROM {$this->name_demo_table} LIMIT {$number_of_users_to_get}"; - $result = $this->dbh->query($sql); - return \tts\database\help_load::pdo_fetch_lazy($result); + $sql = "SELECT * FROM {$this->table} LIMIT {$number_of_users_to_get}"; + $result = $this->pdo->query($sql); + return self::pdo_fetch_lazy($result); } catch (\PDOException $e) { echo \tts\exceptions\DB_Exception::customMessage($e); }