corso https://vola.udemy.com/course/php-mvc-from-scratch/learn/lecture/40931984#overview
filippo.bertilotti
2024-05-23 4dbbdf331906f5e6fba5ae674ea88be7dd633dc1
estrazione del codice in comune nella classe Model (parte 130)
2 files modified
33 ■■■■■ changed files
src/App/Models/product.php 12 ●●●● patch | view | raw | blame | history
src/Framework/Model.php 21 ●●●● 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));