以形式捕获持久关系 [英] Capturing Persistent Relations in a Form
问题描述
我在Persistent中定义了一对多关系,但无法弄清楚如何创建一个可以将其中一个外键作为输入的表单。简化我的用例,如下所示:
Person
name String
Car
personId PersonId
name Text
type Text
现在当我尝试要生成一个Form for Car,personId的字段类型应该是什么?我试过类似的东西,但得到一个错误:
entryForm :: Maybe Car - > Form Car
entryForm car = renderDivs $ Car
< $> areq personIdFieldPersonNothing
*< *> areq textFieldCar Name(carName< $> car)
< areq textFieldType(carType< $> car)
当我运行上述时,错误:不在范围内:`personIdField'。
我试过 intField
,它说:
无法匹配预期的类型`KeyBackend
persistent-1.2.1:Database.Persist.Sql.Types.SqlBackend Person'
与实际类型`文本'
预期类型:字段
m0
(KeyBackend
persistent-1.2.1:Database.Persist.Sql.Types.SqlBackend Person)
实际类型:字段m0文本
在'areq'的第一个参数中,即'intField'
在'(< $>)'的第二个参数中,即
`areq intFieldPersonNothing
理想情况下,我想填充一个人名下拉列表(if没有太多),或者有太多的时候有自由形式的文本字段(例如,自动完成)。关于如何将外键作为用户输入的建议?
更新:
我试过使用selectField,但不确定是否我我正确地做这件事。我仍然遇到错误。首先,我创建了一个where语句来获取personId:
其中
personId = do
person< - runDB $ selectFirst [] [Asc PersonName]
的个案人Just(Entity pId p) - >返回pId
- 无 - > ???
然后我将第一个areq改为
< $> areq(selectField [(First,personId)])Person NameNothing
谢谢! / p>
我能弄清楚如何正确使用selectField。这是我最终做的:
其中
people = do
实体< - runDB $ selectList [] [Asc PersonName]
optionsPairs $ map(\ p - >(personName $ entityVal p,entityKey p))entities
表单字段变为:
< $> areq(selectField people)Person NameNothing
我还没有想出自由表单条目但这是一个好的开始。
I have defined a one-to-many relationship in Persistent but could not figure out how to create a form that can take one of the foreign keys as input. Simplifying my use case to something like this:
Person
name String
Car
personId PersonId
name Text
type Text
Now when I try to generate a Form for Car, what should be the field type for personId? I tried something like this but get an error:
entryForm :: Maybe Car -> Form Car
entryForm car = renderDivs $ Car
<$> areq personIdField "Person" Nothing
<*> areq textField "Car Name" ( carName <$> car)
<*> areq textField "Type" ( carType <$> car)
When I run the above I get the error: Not in scope: `personIdField'.
I tried intField
and it says:
Couldn't match expected type `KeyBackend
persistent-1.2.1:Database.Persist.Sql.Types.SqlBackend Person'
with actual type `Text'
Expected type: Field
m0
(KeyBackend
persistent-1.2.1:Database.Persist.Sql.Types.SqlBackend Person)
Actual type: Field m0 Text
In the first argument of `areq', namely `intField'
In the second argument of `(<$>)', namely
`areq intField "Person" Nothing
Ideally I would like to populate a drop down of Person Names (if there are not too many) or have a free form text field (e.g., with autocomplete) when there are too many. Any suggestions on how I can get foreign key as an input from the user?
Update:
I tried using selectField as follows but not sure if I am doing this correctly. I still get an error. First I created a where statement to get personId:
where
personId = do
person <- runDB $ selectFirst [] [Asc PersonName]
case person of
Just (Entity pId p) -> return pId
-- Nothing -> ???
and then I changed my first areq to
<$> areq (selectField [("First", personId)]) "Person Name" Nothing
Thanks!
I was able to figure out how to use selectField properly. This is what I ended up doing:
where
people = do
entities <- runDB $ selectList [] [Asc PersonName]
optionsPairs $ map (\p -> (personName $ entityVal p, entityKey p)) entities
The form field became:
<$> areq (selectField people) "Person Name" Nothing
I still have not figured out free form entry just yet but this is a good start.
这篇关于以形式捕获持久关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!