【问题标题】:What's the best way to do many-to-many in Persistent Yesod?在 Persistent Yesod 中进行多对多的最佳方式是什么?
【发布时间】:2013-04-18 19:35:58
【问题描述】:

所以我的 /config/models 看起来像这样。

Person
  name Text
Car
  name Text
PersonCar
  personId PersionId eq
  carId CarId eq
  UniquePersonCar personId carId

假设数据库中的输入分别为Person "Batman"Person "Superman"Car "SUV"Car "Ford"

我目前正在这样做以在我的处理程序中将它们链接起来。

runDB $ do
  person <- selectFirst [PersonName ==. "Batman"] []
  car    <- selectFirst [Carname ==. "SUV"] []
  let Entity personId _ = case person of
                            Just info -> infor
                            Nothing -> error "no such Person"
  let Entity carId _ = case car of
                            Just info -> infor
                            Nothing -> error "no such Car"
  _ <- insert $ PersonCar personId carId

有没有更简单的方法来做到这一点?有这样的表达方式吗?

【问题讨论】:

    标签: haskell persistent yesod


    【解决方案1】:

    不,目前还没有这种查询的简写(至少我能想到)。

    【讨论】:

      【解决方案2】:

      调用错误将停止您的应用。 logError 可能会更好。

      这更短:

      import Data.Conduit
      import qualified Data.Conduit.List as DCL
      
      runDB $ do
          mbPersonId <- runResourceT $ selectKeys [PersonName ==. "Batman"] [] $$ DCL.head
          mbCarId    <- runResourceT $ selectKeys [CarName ==. "SUV"] [] $$ DCL.head
      
          case (mbPersonId, mbCarId) of
              (Just personId, Just carId) -> do
                    _ <- insert $ PersonCar personId carId
                    return ()
      
              _ -> $(logError) "error looking for Batman and SUV"
      

      【讨论】:

      • 我在处理程序下做,它给我解析错误,有什么想法吗? postFromR :: Handler RepHtml postFormR = do case result of FormSuccess res -&gt; _ &lt;- runDB$insert$ PersonCar persionId carId _ -&gt; $(logError) "error"
      • @HHC,我在插入行周围添加了一个块,之后需要一个返回表达式。用yesod安装测试,再次抓取代码。检查您的型号名称(PesionId 代替 PersonId,PesonCar 代替 PersonCar)
      猜你喜欢
      • 2016-05-26
      • 1970-01-01
      • 2010-09-10
      • 2012-01-05
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多