src/App/Models/product.php | ●●●●● patch | view | raw | blame | history | |
src/Framework/Model.php | ●●●●● patch | view | raw | blame | history |
src/App/Models/product.php
@@ -6,20 +6,12 @@ use Framework\Model; class Product extends Model { //protected ?string $table = "product"; protected array $errors = []; protected function addError(string $field, string $message): void { $this->errors[$field] = $message; } protected function validate(array $data): bool { protected function validate(array $data): void { if(empty($data["name"])) { $this->addError("name","Name is required"); } return empty($this->errors); } public function getErrors(): array { return $this->errors; } } src/Framework/Model.php
@@ -5,13 +5,26 @@ 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 findAll(): array|bool { @@ -36,7 +49,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));