Removed help_load/save and made model.

main
Robert 3 years ago
parent 620a3fe1d4
commit 6df213be20
  1. 30
      src/classes/database/help_load.php
  2. 71
      src/classes/database/model.php

@ -1,30 +0,0 @@
<?php
declare(strict_types=1);
namespace tts\database;
class help_load {
public static function empty_generator(): \Generator {
yield from [];
}
/**
* Please use this fetch_lazy instead of fetch_all!!!
* To AVOID running out of Memory!!!!!!!!!!!!!!!!!!!!
* @param \PDOStatement $stmt
* @return \Generator
*/
public static function pdo_fetch_lazy(\PDOStatement $stmt): \Generator {
foreach($stmt as $record) {
yield $record;
}
}
public static function is_valid(array $data): bool {
$error_count = (isset($data['errors'])) ? count($data['errors']) : 0;
return ($error_count === 0);
}
}

@ -12,7 +12,7 @@ namespace tts\database;
use \bs_tts\safer_io as SafeIO;
final class help_save {
class model {
use \tts\traits\database\run_sql;
use \tts\traits\database\validation;
@ -23,20 +23,13 @@ final class help_save {
const pre_save_failed = 4;
const post_save_failed = 5;
private $table;
private $members = [];
private $validation_members = [];
private $missing = [];
private $error_message;
private $pdo;
private $primary_key = 'id';
private $db_skiped = [];
public function __construct($pdo, $table) {
$this->pdo = $pdo;
$this->table = $table;
}
public function saved(bool $worked = true): bool {
if (headers_sent()) {
return false;
@ -149,8 +142,6 @@ final class help_save {
return $this->missing;
}
/* From old Model code: */
public function get_member(string $member) {
return $this->members[$member] ?? null;
}
@ -208,6 +199,51 @@ final class help_save {
return $bind;
}
public static function empty_generator(): \Generator {
yield from [];
}
/**
* Please use this fetch_lazy instead of fetch_all!!!
* To AVOID running out of Memory!!!!!!!!!!!!!!!!!!!!
* @param \PDOStatement $stmt
* @return \Generator
*/
public static function pdo_fetch_lazy(\PDOStatement $stmt): \Generator {
foreach($stmt as $record) {
yield $record;
}
}
public static function is_valid(array $data): bool {
$error_count = (isset($data['errors'])) ? count($data['errors']) : 0;
return ($error_count === 0);
}
public function load(int $id): bool{
if (method_exists($this, 'pre_load')) {
$this->pre_load();
}
$sql = "SELECT * FROM {$this->table} WHERE {$this->primaryKey} = ?";
$query = $this->pdo->prepare($sql);
$query->execute(array($id));
if ($query === false) {
return false;
}
$data = $query->fetch(\PDO::FETCH_ASSOC);
if ($data === false) {
return false;
}
$this->members = array_merge($this->members, $data);
if (method_exists($this, 'post_load')) {
$this->post_load();
}
return true;
}
public function insert($table, $info) {
$fields = $this->filter($table, $info);
@ -269,10 +305,9 @@ final class help_save {
* Insert if primary key not set
* Update if primary key set
*/
public function save(object $local_model=null, bool $validate = true): int {
if (method_exists($local_model, 'pre_save')) {
$pre_was_successfull = $local_model->pre_save();
public function save(bool $validate = true): int {
if (method_exists($this, 'pre_save')) {
$pre_was_successfull = $this->pre_save();
if (! $pre_was_successfull) {
return self::pre_save_failed;
}
@ -289,8 +324,8 @@ final class help_save {
}
if (empty($this->members[$this->primary_key])) {
if (method_exists($local_model, 'duplicate_check')) {
$dup = $local_model->duplicate_check();
if (method_exists($this, 'duplicate_check')) {
$dup = $this->duplicate_check();
if ($dup) {
return self::duplicate_found;
}
@ -308,8 +343,8 @@ final class help_save {
));
}
if (method_exists($local_model, 'post_save')) {
$post_save_successfull = $local_model->post_save();
if (method_exists($this, 'post_save')) {
$post_save_successfull = $this->post_save();
if (! $post_save_successfull) {
return self::post_save_failed;
}
Loading…
Cancel
Save