Or provide a default: after map(...is).openOr("").
You can even do openOr(Predef.error("...")) etc. Note that Predef is necessary 
if you imported S.error or S._.

-------------------------------------
Ross Mellgren<dri...@gmail.com> wrote:


I'm not too familiar with mapper, but the probable reason it's giving  
back a box is in case the target object cannot be resolved (e.g. the  
FK is NULL, or something). Box can either contain a single object or  
is empty. In this case you need to pick what to do with an empty box,  
and there are a couple approaches in your situation:

1) Use map to make your venueName function return a Box of String,  
Empty if the venue could not be found, or Full of the venuename if it  
could. This is probably the best bet.

def venueName = venue.obj.map(_.venuename.is)

then calling code either continues to map along, or uses pattern  
matching to extract:

    event.venueName match {
        case Full(name) => do something with the name
        case _ => do something when the name cannot be found
    }

2) Take a function to apply to the venueName if it could be found.  
This is a bit contrived, you'd probably just use approach 1 and map  
the box:

def withVenueName(f: String => Unit) = venue.obj.foreach(venue =>  
f(venue.venuename.is))

which is equivalent to doing approach 1 with a map or foreach, like  
this:

event.venueName.foreach { name => println(name) }

3) just open the box, forcefully. This causes an exception if the box  
is empty, and is usually a bad idea:

def venueName = venue.obj.open_!.venuename.is


Hope that helps,
-Ross


On Jul 26, 2009, at 6:29 PM, Grant Wood wrote:

>
> I'm having a frustrating time trying to access an entity using a
> MappedLongForeignKey based on the example I found in section 6.1.4. of
> the Master.PDF.  This seems like it should be trivial to do, however I
> cannot compile code which appears to be functionally identical to the
> example provided.  Perhaps the document is out of date or someone with
> more experience with Lift can point me in the right direction.
>
> The documentation (master.pdf section 6.1.4 using listing 6.9) states:
> " ... Once we have this defined, accessing the object via the
> relationship is achieved by using the obj method on the foreign key
> field. ..."
>
> Attempting this myself generates a compile error:
> ... /Event.scala:23: error: value venuename is not a member of
> net.liftweb.util.Box[com.subtlesol.model.Location]
>
> (See code fragments below)
>
> venu.obj is returning a Box object, not the Location object I am
> expecting... so clearly it would not be able to access the member
> "venu.obj.venuename" from the Location as the given example would
> suggest.  I understand the compile error, but do not see how to apply
> the example to what I am doing.  What am I missing?
>
>
> My Code:
>
> class Event extends LongKeyedMapper[Event] with IdPK {  ...
>    object venue extends MappedLongForeignKey(this, Location)
>    def venueName = venue.obj.venuename.is  // throws compile error on
> this line
>    ... }
>
> class Location extends LongKeyedMapper[Location] with IdPK { ...
>    object venuename extends MappedPoliteString(this, 40);
>    ... }
>
> Thank You in advance for your help.
>
> >




--~--~---------~--~----~------------~-------~--~----~
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