Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yesod how to show the pure value of a PersistInt64 key

Dog
    name Text
    race Text

getAllDogsR :: Handler Html
getAllDogsR = do
    Dogs<- runDB $ selectList [] [Asc DogName]
    defaultLayout
        [whamlet|
            <ul>
                $forall Entity dogid dog <- Dogs
                    <li>
                        #{show $ unKey (dogid)}
       |]

When I run this code I will get a list of all dog keys that are in my database
like this:

  • PersistInt64 1
  • PersistInt64 2
  • PersistInt64 3
  • PersistInt64 4
  • etc

but what I actually want is to show the pure value of the key
like this:

  • 1
  • 2
  • 3
  • 4
  • etc

My question is how can I achieve this.

like image 972
BARJ Avatar asked Oct 03 '22 11:10

BARJ


1 Answers

You need to extract the key out of a KeyBackend first, like so:

extractKey :: KeyBackend backend entity -> String
extractKey = extractKey' . unKey
  where extractKey' (PersistInt64 k) = show k
        extractKey' _ = ""

You should now be able to do

#{extractKey dogid}
like image 174
Sammy S. Avatar answered Oct 13 '22 10:10

Sammy S.