2014-05-17 2 views
1

나는 다음과 같은 관계를 가지고 말 :양식 입력에서 외래 키 값을 삽입하는 방법은 무엇입니까?

Section 
     name Text 
     UniqueSection name 

    Subject 
     name Text 
     UniqueSubject name 

    Faculty 
     name Text 
     UniqueFaculty name 

    Assignment 
     section SectionId 
     faculty FacultyId 
     subject SubjectId 
     UniqueAssignment section subject 

처리기 :

postNewAssignmentR :: Handler Html 
    postNewAssignmentR = do 

    -- don't know what to use instead xxxField for SectionId/FacultyId/SubjectId 

      sec <- runInputPost $ ireq xxxField "section" 
      fac <- runInputPost $ ireq xxxField "faculty" 
      sub <- runInputPost $ ireq xxxField "subject" 

      runDB $ insert $ Assignment sec fac sub 
      setMessage "Created new Assignment" 
      redirect AssignmentsR 

"섹션", "교수"및 "주제는"텍스트의 값으로 해당 ID가 드롭 다운 입력은 (있는 경우 우리는 runInputPost에서 얻는다.) 어떻게 그들을 sectionId, facultyId, ... 등으로 변환 할까? 또는 다른 방법으로 외래 키를 삽입해야합니까?

답변

1

나는 방금 Yesod를 사용하여 양식을 생성하지 않는다는 것을 깨달았으므로 아래의 나의 원래 제안이 작동하지 않을 수 있습니다. 당신은 아마 Int로 입력 값을 얻을 코드가 아래 경우에 작동하는 경우 다음 fKey = Key $ PersistInt64 intReturnValue

불확실를 사용할 수 있습니다

당신은 제 이름의 목록을 얻을 수 selectField를 사용하여 다음을 채울 수 있습니다 드롭 다운에서 값은 ID에 자동으로 매핑되어야합니다. 입력 양식을 사용하지는 않지만 이와 비슷한 것으로 추측하면됩니다.

<*> ireq (selectField sections) "Section Name" 


where 
sections = do 
    entities <- runDB $ selectList [] [Asc SectionName] 
    optionsPairs $ map (\e -> (sectionName $ entityVal e, entityKey e)) entities 
+0

이 경우 selectField가 작동한다고 가정 할 수 있습니까? – Sam

+0

@Sam : 그것은 .. – Shanthakumar