public static function retrieve($id = null) { $params = array(); $sql = 'SELECT id, username, total, created FROM orders'; if (!is_null($id)) { $sql .= ' WHERE id=:id'; $params['id'] = $id; } $result = Database::select($sql, $params); $orders = array(); foreach ($result as $r) { $orders[] = new Order($r['username'], $r['total'], $r['id'], $r['created']); } if (!is_null($id)) { if (empty($orders)) { throw new \Lib\Exceptions\NotFoundException(); } return $orders[0]; } return $orders; }
public static function retrieve($id = null) { $params = array(); $sql = 'SELECT id, name, price, quantity, image_url, created FROM products'; if (!is_null($id)) { $sql .= ' WHERE id=:id'; $params['id'] = $id; } $result = Database::select($sql, $params); $products = array(); foreach ($result as $r) { $products[] = new Product($r['name'], $r['price'], $r['quantity'], $r['id'], $r['image_url'], $r['created']); } if (!is_null($id)) { if (empty($products)) { throw new \Lib\Exceptions\NotFoundException(); } return $products[0]; } return $products; }
private static function exist($username) { $sql = 'SELECT 1 FROM users WHERE username=:username'; $params = array('username' => $username); $result = Database::select($sql, $params); return count($result) > 0; }