I have a field that holds a datetime as well as a denormalization field to store that same datetime value as a unix timestamp (for reasons that are beyond my control at the moment).

I would like the timestamp field to always be in sync with the "main" datetime field and preferrably even be not writable from outside the methods of that record class (but still be readable and queryable).

I've looked at both LifecycleCallbacks (which should work according some older documentation but now is meant to be used with Field types not Record types) as well as for a special Field type (e.g. AutoField or ComputedField) to no avail.

Should I look into implementing a custom Field, or am I overlooking something obvious? I'm on version Lift 2.6-M2.

class Job private() extends MongoRecord[Job] with ObjectIdKey[Job] {
  ...
  object regDate extends JodaTimeField(this)
  object regDateUnixtime extends LongField(this)
  ...
}
有帮助吗?

解决方案

Have you tried something like

private object regDateUnixtime extends LongField(this)

object regDate extends JodaTimeField(this) {

  override def setBox(in: Box[DateTime]) = {
    super.setBox(in) map { v => 
      regDateUnixtime(v.getMillis) 
      v
    }
  }

}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top