Skip to content

Commit

Permalink
in getByID, product_id should not be null
Browse files Browse the repository at this point in the history
  • Loading branch information
creme332 committed Jun 1, 2024
1 parent 7424b30 commit d17eeef
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions src/models/OrderProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit d17eeef

Please sign in to comment.