trait Posting[MyType <: Mapper[MyType]] { // Defines some common fields for posted user content
self: MyType =>
def primaryKeyField = id
object id extends MappedLongIndex(this)
object creator extends MappedLongForeignKey(this, User)
object createdAt extends MappedLong(this) {
override def defaultValue = System.currentTimeMillis
}
}
class FooPosting extends KeyedMapper[FooPosting] with Posting[MyType] {
....
On Jan 2, 2008, at 4:12 PM, steve.yen wrote:
>
> Sheesh, answering my own question...
>
> I guess if I whack at the keyboard long enough, I'll get either some
> shakespeare or compiling code. Right now code's good for me. Below
> is a compiling version of the what I kind of wanted in having a shared
> Model base trait.
>
> In copy-&-paste terms, the primaryKeyField and id still needs to be
> defined in every model subclass. I can live with that, though.
>
> And, a new issue: now I'm wondering how can I get "typed" foreign
> keys, where the foreign key is a composite of two columns, like
> parent_type and parent_id, like in Rails. For example, I would like
> to have Comment records point to either Reviews or to ForumTopics.
Can't do that directly... mainly because of type safety issues.
Each foreign key has to point to a specific type. You could build a
utility method on top to return one of the other depending on the value.
For example:
object reviewParent extends MappedLongForeignKey(this, Review)
object forumParent extends MappedLongForeignKey(this, ForumTopic)
object parentType extends MappedEnum(this, Parent)
def realParent: /* insert a shared base type here */ = parentType.is
match {
case ReviewParent => revientParent.obj
case ForumParent => forumParent.obj
case _ => Empty
}
Not overly clean, but if there's call for making that into some form
of mixin, it's possible.
Thanks,
David
--
David Pollak
http://blog.lostlake.org
http://liftweb.net
Collaborative Task Management http://much4.us