Mapper save on MappedLongForeignKey

73 views
Skip to first unread message

Brent Lemons

unread,
Jun 24, 2011, 6:01:25 PM6/24/11
to Lift
Is there a way to cascade a save when I have an object that extends
MappedLongForeignKey?

For example, let's say I have the following relationship:

val myName: Name = Name.create.given("test").family("name")
val myPerson: Person = Person.create.name(myName)

If I say myPerson.save, it inserts a null value in the person table
for the name. If I say myName.save prior to assigning it to the name
in person, then everything is great.

I'd rather not have to insert each entity individually. Is there a
way to cascade the insert of the name object?

Thanks,
Brent

Naftoli Gugenheim

unread,
Jun 24, 2011, 6:08:18 PM6/24/11
to lif...@googlegroups.com
One option: Make your FK mix in LifecycleCallbacks and override def afterSave {obj.foreach(_.save)}
Not sure how that interacts with save's success return value. Another option: in the Mapper class,

override def save = super.save && fk.obj.dmap(true)(_.save)

This will return true if super.save returns true, and also either fk.obj is empty or its mapper's save returns true.
It will return false if either super.save returns false, or if both fk.obj is full and its mapper's save returns false.
It will only try to save the foreign mapper if super.save returns true.



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


Brent Lemons

unread,
Jun 24, 2011, 6:15:38 PM6/24/11
to Lift
Thank you for the quick reply! I'll give both a shot and see what
works best for me.




On Jun 24, 5:08 pm, Naftoli Gugenheim <naftoli...@gmail.com> wrote:
> One option: Make your FK mix in LifecycleCallbacks and override def
> afterSave {obj.foreach(_.save)}
> Not sure how that interacts with save's success return value. Another
> option: in the Mapper class,
>
> override def save = super.save && fk.obj.dmap(true)(_.save)
>
> This will return true if super.save returns true, and also either fk.obj is
> empty or its mapper's save returns true.
> It will return false if either super.save returns false, or if both fk.obj
> is full and its mapper's save returns false.
> It will only try to save the foreign mapper if super.save returns true.
>
> On Fri, Jun 24, 2011 at 6:01 PM, Brent Lemons
> <brent.lem...@zoicrecords.com>wrote:
Reply all
Reply to author
Forward
0 new messages