diff --git a/src/models/OrderProduct.php b/src/models/OrderProduct.php index 5c4d7be..efb4ce4 100644 --- a/src/models/OrderProduct.php +++ b/src/models/OrderProduct.php @@ -87,35 +87,33 @@ public function validate(): array return $errors; } - public static function getByID(int $order_id, int $product_id = null): ?OrderProduct + /** + * order_id and product_id are the primary of the record to be searched. + * @param int $order_id + * @param int $product_id + * @return OrderProduct|null + */ + public static function getByID(int $order_id, int $product_id): ?OrderProduct { - $query = 'SELECT * FROM order_product WHERE order_id = :order_id'; - $params = ['order_id' => $order_id]; - - if ($product_id !== null) { - $query .= ' AND product_id = :product_id'; - $params['product_id'] = $product_id; - } + $query = 'SELECT * FROM order_product WHERE order_id = ? and product_id= ?'; + $params = [$order_id, $product_id]; - $result = self::query($query, $params); - if (empty($result)) { - return null; - } + $result = self::query($query, $params); + if (empty($result)) { + return null; + } - // Assuming there's only one product for a given order if product_id is not provided - if ($product_id === null) { $result = $result[0]; - } - return new OrderProduct( - product_id: $result->product_id, - cup_size: $result->cup_size, - milk_type: $result->milk_type, - quantity: $result->quantity, - unit_price: (float)$result->unit_price, - order_id: $result->order_id, - ); - } + return new OrderProduct( + product_id: $result->product_id, + cup_size: $result->cup_size, + milk_type: $result->milk_type, + quantity: $result->quantity, + unit_price: (float)$result->unit_price, + order_id: $result->order_id, + ); + } public function getOrderID(): int {