44 lines
1.2 KiB
PHP
44 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Core\Database\Database;
|
|
use PDO;
|
|
|
|
class OrderItem
|
|
{
|
|
public static function getByOrderId(int $orderId): array
|
|
{
|
|
$db = Database::getConnection();
|
|
$stmt = $db->prepare("
|
|
SELECT oi.*, p.sku, p.category
|
|
FROM order_items oi
|
|
LEFT JOIN products p ON oi.product_id = p.id
|
|
WHERE oi.order_id = ?
|
|
ORDER BY oi.id ASC
|
|
");
|
|
$stmt->execute([$orderId]);
|
|
return $stmt->fetchAll();
|
|
}
|
|
|
|
public static function create(array $data): int
|
|
{
|
|
$db = Database::getConnection();
|
|
$stmt = $db->prepare("
|
|
INSERT INTO order_items (order_id, product_id, product_name, quantity, unit_price, total_price)
|
|
VALUES (:order_id, :product_id, :product_name, :quantity, :unit_price, :total_price)
|
|
");
|
|
$stmt->execute([
|
|
':order_id' => $data['order_id'],
|
|
':product_id' => $data['product_id'],
|
|
':product_name' => $data['product_name'],
|
|
':quantity' => $data['quantity'],
|
|
':unit_price' => $data['unit_price'],
|
|
':total_price' => $data['total_price'],
|
|
]);
|
|
return (int)$db->lastInsertId();
|
|
}
|
|
}
|