File manager - Edit - /home/webapp69.cm.in.th/u69319090039/Shop39/public/app/Models/Product.php
Back
<?php class Product extends Model { protected string $table = 'products'; public function getFeaturedProducts(int $limit = 8): array { $sql = "SELECT p.*, c.name AS category_name, sp.shop_name, sp.slug AS shop_slug, (SELECT image_path FROM product_images WHERE product_id = p.id AND is_primary = 1 LIMIT 1) AS primary_image FROM products p JOIN categories c ON p.category_id = c.id JOIN seller_profiles sp ON p.seller_id = sp.id WHERE p.status = 'active' ORDER BY p.id DESC LIMIT :limit"; $stmt = $this->db->prepare($sql); $stmt->bindValue(':limit', $limit, PDO::PARAM_INT); $stmt->execute(); return $stmt->fetchAll(); } public function getProductWithDetails(string $slug): ?array { $sql = "SELECT p.*, c.name AS category_name, sp.shop_name, sp.slug AS shop_slug, sp.logo AS shop_logo FROM products p JOIN categories c ON p.category_id = c.id JOIN seller_profiles sp ON p.seller_id = sp.id WHERE p.slug = :slug AND p.status = 'active' LIMIT 1"; $stmt = $this->db->prepare($sql); $stmt->execute(['slug' => $slug]); $product = $stmt->fetch(); if (!$product) return null; // Fetch images $imgStmt = $this->db->prepare("SELECT * FROM product_images WHERE product_id = :pid"); $imgStmt->execute(['pid' => $product['id']]); $product['images'] = $imgStmt->fetchAll(); // Fetch options with their values $optStmt = $this->db->prepare("SELECT * FROM product_options WHERE product_id = :pid ORDER BY sort_order ASC, id ASC"); $optStmt->execute(['pid' => $product['id']]); $options = $optStmt->fetchAll(); foreach ($options as &$opt) { $valStmt = $this->db->prepare("SELECT * FROM product_option_values WHERE option_id = :oid ORDER BY sort_order ASC, id ASC"); $valStmt->execute(['oid' => $opt['id']]); $opt['values'] = $valStmt->fetchAll(); } unset($opt); $product['options'] = $options; // Fetch variants with linked option_value_ids $varStmt = $this->db->prepare("SELECT * FROM product_variants WHERE product_id = :pid ORDER BY id ASC"); $varStmt->execute(['pid' => $product['id']]); $variants = $varStmt->fetchAll(); foreach ($variants as &$var) { $vovStmt = $this->db->prepare("SELECT option_value_id FROM variant_option_values WHERE variant_id = :vid"); $vovStmt->execute(['vid' => $var['id']]); $var['option_value_ids'] = $vovStmt->fetchAll(PDO::FETCH_COLUMN); } unset($var); $product['variants'] = $variants; return $product; } public function getFullProductData(int $productId): ?array { $stmt = $this->db->prepare("SELECT * FROM products WHERE id = :id"); $stmt->execute(['id' => $productId]); $product = $stmt->fetch(); if (!$product) return null; $imgStmt = $this->db->prepare("SELECT * FROM product_images WHERE product_id = :pid ORDER BY is_primary DESC, id ASC"); $imgStmt->execute(['pid' => $productId]); $product['images'] = $imgStmt->fetchAll(); $optStmt = $this->db->prepare("SELECT * FROM product_options WHERE product_id = :pid ORDER BY sort_order ASC, id ASC"); $optStmt->execute(['pid' => $productId]); $options = $optStmt->fetchAll(); foreach ($options as &$opt) { $valStmt = $this->db->prepare("SELECT * FROM product_option_values WHERE option_id = :oid ORDER BY sort_order ASC, id ASC"); $valStmt->execute(['oid' => $opt['id']]); $opt['values'] = $valStmt->fetchAll(); } unset($opt); $product['options'] = $options; $varStmt = $this->db->prepare("SELECT * FROM product_variants WHERE product_id = :pid ORDER BY id ASC"); $varStmt->execute(['pid' => $productId]); $variants = $varStmt->fetchAll(); foreach ($variants as &$var) { $vovStmt = $this->db->prepare("SELECT option_value_id FROM variant_option_values WHERE variant_id = :vid"); $vovStmt->execute(['vid' => $var['id']]); $var['option_value_ids'] = $vovStmt->fetchAll(PDO::FETCH_COLUMN); } unset($var); $product['variants'] = $variants; return $product; } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.24 |
proxy
|
phpinfo
|
Settings