Product Repository
public function updateProduct(Product $product)
{
$sql = "UPDATE products SET
name= :name,
price = :price,
description= :description,
type_product= :type_product
WHERE id = :id";
$stm = $this->pdo->prepare($sql);
$stm->execute([
':name' => $product->getName(),
':price' => $product->getPrice(),
':description' => $product->getDescription(),
':type_product' => $product->getTypeProduct(),
':id' => $product->getId()
]);
if($product->getImage() !== null){
$this->updateImage($product);
}
}
public function updateImage (Product $product)
{
$sql = "UPDATE products SET image = :image WHERE id = :id";
$stm = $this->pdo->prepare($sql);
$stm->execute([
':image' => $product->getImage(),
':id' => $product->getId()
]);
}
public function uploadImage(array $image, Product $product){
if (isset($image['image']) && $image['image']['error'] === UPLOAD_ERR_OK) {
$imageName = uniqid('product_' . $product->getId() . '_') . '.' . pathinfo($image['image']['name'], PATHINFO_EXTENSION);
$product->setImage($imageName);
$uploadPath = $product->getImagePath();
if (move_uploaded_file($image['image']['tmp_name'], $uploadPath)) {
return true;
}
}
return false;
}
editProduct
$productRepo->uploadImage($_FILES, $product);
$productRepo->updateProduct($product);