corso https://vola.udemy.com/course/php-mvc-from-scratch/learn/lecture/40931984#overview
filippo.bertilotti
2024-05-23 bd886f4a0f3f996295b0beb07a5b14b3b1b089f9
inizializzazione metodo update che permette di processare dati messi nella edit page (parte 135)
2 files modified
38 ■■■■■ changed files
src/App/Controllers/products.php 28 ●●●●● patch | view | raw | blame | history
src/Framework/Model.php 10 ●●●●● patch | view | raw | blame | history
src/App/Controllers/products.php
@@ -69,4 +69,32 @@
            ]);
        };
    }
    public function update(string $id) {
        $product = $this->model->find($id);
        if( $product == false ) {
            throw new PageNotFoundException("Product not found");
        }
        $data = [
            "name" => $_POST["name"],
            "description" => empty($_POST["description"]) ? null : $_POST["description"]
        ];
        if($this->model->update($id, $data)) {
            header("Location: /products/{$id}/show");
            exit;
        } else {
            echo $this->viewer->render("shared/header.php", [
                "title" => "Edit Product"
            ]);
            echo $this->viewer->render("Products/edit.php", [
                "errors" => $this->model->getErrors(),
                "product"=> $product
            ]);
        };
    }
}
src/Framework/Model.php
@@ -13,6 +13,16 @@
        public  function __construct(private Database $database) {
        }
        public function update(string $id, array $data): bool {
            $this->validate($data);
            if(!empty($this->errors)) {
                return false;
            }
            return true;
        }
        protected function addError(string $field, string $message): void {
            $this->errors[$field] = $message;