Hallo zusammen,
meine Warenkorb-Klasse hat u.a. folgende zwei Methoden:
Damit mir die Eigenschaften wie MinSaleQty, MaxSaleQty oder die Methode validateProduct() Werte zurückliefern, setze ich zunächst mit $product->setInfo() einen Query ab, welcher mir die entsprechenden Daten zum Produkt aus der Datenbank holt und anschließend in meiner Produkt-Klasse einem Array zuweist. Der Query sieht wie folgt aus:
Jetzt meine eigentliche Frage. Wäre besser, wenn ich einzelne Querys für jeden Eigenschaft / Methode erzeuge oder stattdessen die allumfassende Query beibehalte. Ehrlich gesagt habe ich keine Ahnung, was besser ist. Ein großer Query oder mehrere kleine Querys?
meine Warenkorb-Klasse hat u.a. folgende zwei Methoden:
PHP-Code:
public function add($product_id, $quantity) {
$product = new Product($product_id);
$product->setInfo();
try {
if ($product->validateProduct()) {
if (isset($quantity) && intval($quantity) != 0) {
$quantity = intval($quantity);
if (isset($this->items[$product_id])) {
$this->items[$product_id]['quantity'] += $quantity;
}
else {
$this->items[$product_id]['quantity'] = $quantity;
}
if ($this->items[$product_id]['quantity'] < $product->getMinSaleQty()) {
return $this->items[$product_id]['quantity'] = $product->getMinSaleQty();
}
if ($this->items[$product_id]['quantity'] > $product->getMaxSaleQty()) {
return $this->items[$product_id]['quantity'] = $product->getMaxSaleQty();
}
}
else {
throw new Exception('Ungültige Mengenangabe.');
}
}
else {
throw new Exception('Ungültiges Produkt.');
}
}
catch (Exception $e) {
$_SESSION['error'] = $e->getMessage();
}
}
public function update($product_id, $quantity) {
$product = new Product($product_id);
$product->setInfo();
if (array_key_exists($product_id, $this->getItems())) {
$this->items[$product_id]['quantity'] += $quantity;
if ($this->items[$product_id]['quantity'] < $product->getMinSaleQty()) {
return $this->items[$product_id]['quantity'] = $product->getMinSaleQty();
}
if ($this->items[$product_id]['quantity'] > $product->getMaxSaleQty()) {
return $this->items[$product_id]['quantity'] = $product->getMaxSaleQty();
}
}
}
PHP-Code:
protected function _queryInfo() {
$sql = 'SELECT p.product_id, p.price, p.weight, p.image_src,
pd.name, pd.abstract, pd.description, pd.ingredients,
ps.min_sale_qty, ps.max_sale_qty
FROM product AS p
LEFT JOIN product_description AS pd
ON pd.product_id = p.product_id
LEFT JOIN product_stock AS ps
ON ps.product_id = p.product_id
WHERE p.product_id = :product_id
AND pd.lang = :lang';
$db = Database::getInstance()->getConnection();
$pstmt = $db->prepare($sql);
$pstmt->bindParam(':lang', $GLOBALS['lang']);
$pstmt->bindParam(':product_id', $this->productId);
$pstmt->execute();
return $pstmt->fetch(PDO::FETCH_ASSOC);
}
Kommentar