On Wed, Aug 19, 2009 at 9:12 AM, Hannes <hannes.flo...@gmx.li> wrote:

>
> Hey Lifters,
>
> Again and again I'm having problems to do simple CRUD operations with
> mapped fields, especially referenced fields (foreign keys).
>
> Because I think everyone need those things and I couldn't find a simple
> way to do it (I still don't really know how to use .map(...) with openOr
> to get foreign key references out of the database...it's like stumbling
> around in the dark...), I would write an article for the wiki.
>
> For example, I've a class that extends LongKeyedMapper and mixes in a
> trait (that extends BaseLongKeyedMapper). When I want to access a
> foreign key that is defined inside the trait, I can't do it the way that
> is described in the Lift Book (from 10th of July) in Listing 6.9:
> Accessing Foreign Objects! Why?


First, please take a look at
http://blog.lostlake.org/index.php?/archives/50-The-Scala-Option-class-and-how-lift-uses-it.html
It's a discussion about Option, which provides a subset of what Lift's Box
provides.  It's also based on Scala 2.3 syntax... but you'll get the idea of
what Option/Box is all about.

So, if you've got a LongKeyedMapper that is a target of a foreign key
reference from another Mapper class:

class Taget extends LongKeyedMapper[Target] with IdPK {
...

  object name extends MappedString(this, 64)
}

class RefersTo extends Mapper[RefersTo] {
  object target extends MappedLongForeignKey(this, Target)
}

So, if I've got a RefersTo instance and I want to get the name from the
target foreign key reference:

val rt: RefersTo = ...
val target: Box[Target] = rt.target.obj // load the FK reference, if you can
val nameBox: Box[String] = target.map(_.name.is) // you need the "is" part
so you get the String, not the MappedString
val name: String = nameBox openOr "N/A"

But we can shorten the whole thing to:

val name = rt.target.obj.map(_.name.is) openOr "N/A"


In general, when you're starting out, write out the complex expressions as
separate lines with the types explicitly declared.  Then refactor over and
over to make the code more concise.  It might be worthwhile to leave the
original code in a comment so you can remind yourself the "exploded" view of
the expression.


>
> thanks
>
> >
>


-- 
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to liftweb@googlegroups.com
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to