11

我将 Persistent orm 与 scotty web 框架一起使用。

我想通过 id 从 db 中获取价值。这些 id 来自 GET 请求

有一个“get”函数接受“Key Entity”变量并返回“Maybe Entity”。

我使用以下代码从数据库中获取价值

k <- keyFromValues $ [(PersistInt64 myOwnIntVarFromRequest)]
case k of
    Left _ -> {-some processing-}
    Right x -> do
    t <- liftIO . runDb $ get (x::Key Post) --Post is one of my models
    case t of
        Nothing -> {-processing-}
        Just x -> {-processing-}

这些代码非常丑陋。但我不知道如何做得更好

所以我的问题是如何在不调用 keyFromValues 的情况下获取“Key Entity”类型的变量。

PS对不起我的英语不好

4

1 回答 1

15

您可以为此使用toSqlKey

于 2015-01-22T01:25:54.397 回答