Product:
<?php
class Product
{
private string $productName;
private string $sku;
private float $price;
private string $productType;
public function __construct()
{
$this->productName = isset($_POST['name']) ?
(string
) $_POST['name'] : null; $this->sku = isset($_POST['sku']) ?
(string
) $_POST['sku'] : null; $this->price = isset($_POST['price']) ?
(float
) $_POST['price'] : null; $this->productType = isset($_POST['productType']) ?
(string
) $_POST['productType'] : null; }
function ensureNoEmpytValues()
{
if (empty($this->productName) || empty($this->sku) || empty($this->price) || empty($this->productType)) return false;
return true;
}
}
Oraz ProductAdd:
<?php
namespace nameOne;
use Product;
class ProductAdd extends Product
{
private DB $db;
public function __construct(DB $db){
$this->db = $db;
// parent::__construct();
}
public function listProperties()
{
$properties = $this->getDistinctProperties();
return $properties;
}
private function getDistinctProperties()
{
// $db = new DB();
$this->db->query("SELECT DISTINCT type FROM property ORDER BY type ASC");
$results = $this->db->resultSet();
return $results;
}
public function listFullProperties()
{
$properties = $this->getFullProperties();
return $properties;
}
private function getFullProperties()
{
// $db = new DB();
$this->db->query("SELECT * FROM property ORDER BY type ASC");
$results = $this->db->resultSet();
return $results;
}
public function groupFullProperties
(): array {
foreach ($this->getFullProperties() as $row) {
$property = $row['property'];
$newProperty = $row['type'];
if (!isset($properties[$newProperty])) { $properties[$newProperty] = array(); }
$properties[$newProperty][] = [
'id' => $row['id'],
'property' => $property,
'label' => $row['label'],
'description' => $row['description']
];
}
return $properties;
}
public function checkExistingSKU(string $skuToCheck)
{
// $db = new DB();
$this->db->query("SELECT sku FROM product WHERE sku = :sku");
$this->db->bind(':sku', $skuToCheck);
if ($this->db->single()) {
return true;
}
return false;
}
public function insertNewProduct()
{
/* to na razie jest zakomentowane żebym w ogóle widział jakie są błędy*/
// if ($this->ensureNoEmpytValues() === false)
// return false;
$sku = $this->sku;
$productName = $this->productName;
$price = $this->price;
$selectedType = $this->productType;
$properties = $this->groupFullProperties();
foreach ($properties[$selectedType] as $propertyData) {
$postIndex = $propertyData['property'];
if (empty($_POST[$postIndex])) return false;
}
$queryInsertProduct = "INSERT INTO `product` (`id`, `sku`, `name`, `price`, `type`) VALUES (null, :sku, :productName , :price, :selectedType)";
$this->db->query($queryInsertProduct);
$this->db->bind(':sku', $sku);
$this->db->bind(':productName', $productName);
$this->db->bind(':price', $price);
$this->db->bind(':selectedType', $selectedType);
$this->db->execute();
$lastProductId = $this->db->lastInsertId();
foreach ($properties[$selectedType] as $propertyData) {
$propertyId = (int) $propertyData['id'];
$productValue = $_POST[(string) $propertyData['property']];
$queryInsertProductProperty = "INSERT INTO `product_property` (`id`, `product_id`, `property_id`, `value`) VALUES (null, $lastProductId , $propertyId, :productValue )";
$this->db->query($queryInsertProductProperty);
$this->db->bind(':productValue', $productValue);
$this->db->execute();
}
}
}
?>
Przyczepia się że nie wie co to jest:
$sku = $this->sku;
$productName = $this->productName;
$price = $this->price;
$selectedType = $this->productType;
Jak w product zmienię na public to za to przyczepia się że Product::$sku must not be accessed before initialization
Ten post edytował sadistic_son 12.01.2023, 15:26:52