Web ページにアイテムのリストを、別のテーブルの関連する詳細と共に (多対 1 の関係で) 表示したいと考えています。Yesod でこれを行うにはどうすればよいですか? デフォルトの足場を使用しています。結果はrunDB
、コンテキスト内でネストできないということですWidgetT
— またはそう思います。
これをより具体的にするfeaturesAssociatedWith
ために、次のハムレット コードで使用する関数をどのように定義すればよいでしょうか。
<h2> Cars
$forall Entity carId car <- carList
<div class="car-item">
<h3> #{carYear car} #{carMake car} #{carModel car}
<ul>
$forall feature <- featuresAssociatedWith carId
<li> #{feature}
次のモデルがあるとします。
Car
make Text
model Text
year Int
CarFeature
car CarId
text Text
UniqueCF car text
これが現在のハンドラー関数です
getCarListR :: Handler Html
getCarListR = do
carList <- runDB $ selectList [] [Asc CarOrder]
liftIO $ print $ length carList
defaultLayout $ do
setTitle "Cars"
$(widgetFile "carList")
この方法でウィジェットにクエリを埋め込むのが最も自然に思えrunDB
ますが、これも不可能です。
featuresAssocWith :: CarId -> [Entity CarFeature]
featuresAssocWith carID = selectList [CarFeatureCar ==. carID] []