No dobra, dotarło. Czyli teraz tak...
To moja funkcja zwracająca pogrupowane properties:
public function groupFullProperties
(): array {
foreach ($this->getFullProperties() as $row) {
$property = $row['property'];
$newProperty = $row['type'];
if (!isset($properties[$newProperty])) { $properties[$newProperty] = array(); }
$properties[$newProperty][] = [
'property' => $property,
'label' => $row['label'],
'description' => $row['description']
];
}
return $properties;
}
Ona zwraca tablicę wyglądającą tak:
Kod
Array
(
[Book] => Array
(
[0] => Array
(
[property] => weight
[label] => Weight (kg)
[description] => Please provide the weight of book in kg.
)
)
[DVD] => Array
(
[0] => Array
(
[property] => size
[label] => Size (MB)
[description] => Please provide size of DVD in MB.
)
)
[Furniture] => Array
(
[0] => Array
(
[property] => width
[label] => Width (cm)
[description] => Please provide dimentions in WxHxL format.
)
[1] => Array
(
[property] => height
[label] => Height (cm)
[description] => Please provide dimentions in WxHxL format.
)
[2] => Array
(
[property] => length
[label] => Length (cm)
[description] => Please provide dimentions in WxHxL format.
)
)
)
Ja stąd potrzebuję jedynie 'properties', czyli tablicę która będzie wyglądać tak:
$types = ['dvd' => [0=>['property' => 'size']], 'furniture' => [0=>['property' => 'height'] , 1=>['property'=>'width'], 2=>['property'=>'lenght']], 'book' => [0=>['property'=>'weight']]];
Wygląda nieco inaczej niż to co podałeś, ale myślę, że warto użyć już istniejącej metody zamiast pisać nową, robiącą niemalże to samo (don't repeat yourself

).
Teraz kod realizujący dodawanie. Na razie bezczelnie wrzucam input od usera, bez czyszczenia POST, bez bindowania, itd.
$db = new DB();
$name = $_POST['name'];
$selectedType = $_POST['productType'];
$queryInsertProduct = "INSERT INTO `product` (`id`, `sku`, `name`, `price`, `type`) VALUES (null, '$_POST[sku]', '$name' , '$_POST[price]', '$selectedType')";
$db->query($queryInsertProduct);
$db->execute();
$lastProductId = $db->lastInsertId();
$properties = $this->groupFullProperties();
foreach ($properties[$selectedType] as $type => $properties) {
foreach ($properties as $index => $propertyData) {
if ($index == 'property') {
$queryPropertyType = "SELECT `id` FROM `property` WHERE `property` = '$propertyData'";
$db->query($queryPropertyType);
$propertyId = $db->single();
$postIndex = $propertyData;
$queryInsertProductProperty = "INSERT INTO `product_property` (`id`, `product_id`, `property_id`, `value`) VALUES (null, $lastProductId , $propertyId[id], '$_POST[$postIndex]')";
$db->query($queryInsertProductProperty);
$db->execute();
}
}
}
No i teraz problem jest taki, że user może wpisać dane dla innego typu, np poda typ 'dvd' ale wpisze właściwości dla mebla (wysokość, szerokość). Tak więc produkt w takim przypadku nie powinien się w ogóle dodawać (pierwszy insert). Do pętli go wrzucić nie mogę, bo nie ważne ile jest właściwości properties, to produkt musi się dodać tylko raz. Jakieś sugestie jak z tego wybrnąć?