1

我在 Yesod 应用程序中定义了以下模型:

CvCategory
    title Text
    order Int
    UniqueCvCategory title

CvItem
    category CvCategoryId
    title Text
    fromYear Int
    toYear Int Maybe
    description Text Maybe
    UniqueCvItem title

我在处理程序中有以下查询:

cvcategories <- selectList [] [] --all cv categories

在我的小村庄模板中,我想做类似的事情:

<ul>
$forall cvcategory <- cvcategories
    <li>$#{cvCategoryTitle cvcategory}
         $forall cvitem <- cvcategory --how?
         <li>#{cvItemTitle cvitem}

在 Django 中,您可以轻松定义 a related_name,然后使用它轻松访问所有“子对象”。Yesod也有可能吗?如何?

4

1 回答 1

2

更改您的查询,例如

do
  cvcategories <- selectList [] [] --all cv categories
  forM cvcategories $ \cat -> do -- for each category
    cvitems <- selectList [CvCategoryId .== entityKey cat] -- all items belonging to it
    return (cat, cvitems) -- return tuple of category and its items

然后你的哈姆雷特看起来像

<ul>
$forall (cvcategory, cvitems) <- cvcategories
    <li>$#{cvCategoryTitle cvcategory}
         $forall cvitem <- items
         <li>#{cvItemTitle cvitem}
于 2018-06-20T12:28:34.087 回答