Haskell 如何在应用程序窗体上为YesSOD中的外键定义字段?
如果我们在模型文件中定义了2个简单对象,例如:-Haskell 如何在应用程序窗体上为YesSOD中的外键定义字段?,haskell,yesod,Haskell,Yesod,如果我们在模型文件中定义了2个简单对象,例如:- Person name Text Age Int Book title Text author Text 我们可以将Book的应用程序表单定义为:- addBookForm = renderDivs $ Book <$> areq textField "title" Nothing <*> areq textField "author" Nothing 则无法编译上述表单,并出现以下错误:- C
Person
name Text
Age Int
Book
title Text
author Text
我们可以将Book的应用程序表单定义为:-
addBookForm = renderDivs $ Book
<$> areq textField "title" Nothing
<*> areq textField "author" Nothing
则无法编译上述表单,并出现以下错误:-
Couldn't match expected type `KeyBackend Database.Persist.GenericSql.Raw.SqlBackend Person' with actual type `Text'
Expected type: Field
sub0
master0
(KeyBackend Database.Persist.GenericSql.Raw.SqlBackend Person)
Actual type: Field sub0 master0 Text
In the first argument of `areq', namely `textField'
In the second argument of `(<*>)', namely
`areq textField "author" Nothing'
无法将预期类型“KeyBackend Database.Persist.GenericSql.Raw.SqlBackend Person”与实际类型“Text”匹配
预期类型:字段
sub0
master0
(KeyBackend Database.Persist.GenericSql.Raw.SqlBackend个人)
实际类型:字段sub0 master0 Text
在'areq'的第一个参数中,即'textField'
在“()”的第二个参数中,即
`areq文本字段“作者”为“无”
现在如何定义作者字段?
我们需要使用一元形式吗
谢谢 错误消息表示您试图使用文本(来自字段结果)作为键 您可以使用
checkMMap
包装文本字段并修改结果:
addBookForm = renderDivs $ Book
<$> areq textField "title" Nothing
<*> (entityKey <$> areq authorField "author" Nothing)
where
authorField = checkMMap findAuthor (personName . entityVal) textField
findAuthor name = do
mperson <- runDB $ selectFirst [PersonName ==. name] []
case mperson of
Just person -> return $ Right person
Nothing -> return $ Left ("Person not found." :: Text)
然后选择First…而不是
mperson <- runDB $ getBy $ UniquePerson name
mperson
Person
name Text
...
UniquePerson name
mperson <- runDB $ getBy $ UniquePerson name