A Sample PHP Project for the tts_framework.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

72 lines
2.4 KiB

<?php
namespace prj\{{.Root}}\models\{{.Subfolder}};
class {{.File}}_model {
private $dbh;
private $demo_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 pre_save(): bool {
$missing = $this->db_helper->get_missing();
if (count($missing)) {
echo "Required fields not set " . implode(",", $missing) . "!";
return false;
}
if ($this->db_helper->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}`(
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`first_name` TEXT,
`last_name` TEXT
)";
$this->dbh->query($create_table_sql);
}
public function populate($rows): void {
$dummy = new \tts\database\dummy_data($this->dbh);
$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);
}
public function save_{{.Method}}(array $input): bool {
$set = $this->db_helper->set_members_by_array($input);
if ($set === false) {
return false; // Found Validation issues in input
}
try {
$status = $this->db_helper->save($this);
} catch (\PDOException $e) {
echo \tts\exceptions\DB_Exception::customMessage($e, "Unable to save changes!");
}
if ($status === $this->db_helper::successful_save) {
return true; // It saved
}
return false; // It did not save
}
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();
}
try {
$sql = "SELECT * FROM {$this->demo_table} LIMIT {$number_of_users_to_get}";
$result = $this->dbh->query($sql);
return \tts\database\help_load::fetch_lazy($result);
} catch (\PDOException $e) {
echo \tts\exceptions\DB_Exception::customMessage($e);
}
}
}