From 85f6ec1bc8f094e769835e9836b9fab7fb9e6ace Mon Sep 17 00:00:00 2001
From: filippo.bertilotti <filippobertilotti@gmail.com>
Date: Thu, 23 May 2024 11:18:21 +0200
Subject: [PATCH] output dei valori giĆ  esistenti per modificarli nella pagina edit (parte 134)

---
 src/Framework/Model.php |   23 +++++++++++++++++++++--
 1 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/src/Framework/Model.php b/src/Framework/Model.php
index 39ca40f..51f28c2 100644
--- a/src/Framework/Model.php
+++ b/src/Framework/Model.php
@@ -5,13 +5,30 @@
     use PDO;
     abstract class Model
     {
+        protected array $errors = [];
         protected ?string $table;
+
+        abstract protected function validate (array $data): void;
+        
+        public  function __construct(private Database $database) {
+        }
+
+
+        protected function addError(string $field, string $message): void {
+            $this->errors[$field] = $message;
+        }
+
+        public function getErrors(): array {
+            return $this->errors;
+        }
 
         private function getTable() {
             $parts = explode("\\", $this::class);
             return strtolower(array_pop($parts));
         }
-        public  function __construct(private Database $database) {
+        public function getInsertID(): string {
+            $conn = $this->database->getConnection();
+            return $conn->lastInsertId();
         }
         public function findAll(): array|bool
         {
@@ -36,7 +53,9 @@
         }
 
         public function insert(array $data) : bool {
-            if( ! $this->validate($data)) {
+            $this->validate($data);
+            
+            if(!empty($this->errors)) {
                 return false;
             }
             $columns = implode(", " , array_keys($data));

--
Gitblit v1.8.0