我尝试使用过滤器中的日期时间列[DateTime],但是无法弄清楚如何使其工作。
import play.api.Play.current
import play.api.db.slick.Config.driver.simple._
import play.api.db.slick._
import org.joda.time.{DateTime, DateTimeComparator, DateTimeZone}
private val dateComparator = DateTimeComparator.getInstance()
implicit def dateTimeToScalaWrapper(dt: DateTime): DateTimeWrapper = new DateTimeWrapper(dt)
class DateTimeWrapper(dt: DateTime) extends Ordered[DateTime] with Ordering[DateTime] {
def compare(that: DateTime): Int = dateComparator.compare(dt, that)
def compare(a: DateTime, b: DateTime): Int = dateComparator.compare(a, b)
}
implicit def jodaType = MappedColumnType.base[DateTime, Timestamp](
{d => new Timestamp(d.getMillis)} ,
{d => new DateTime(d.getTime, UTC)}
)
def removeTimeSlots(start: DateTime, end: DateTime, reservationId: Long) : Int = DB withTransaction { implicit session =>
timeSlots.filter(x => x.reservationId == reservationId && x.startTime >= start && x.starTime <= end).delete
}
在我比较x.startTime并开始时,我得到以下错误:
Error:(90, -1) Play 2 Compiler:
Reservations.scala:90: polymorphic expression cannot be instantiated to expected type;
found : [R]scala.slick.lifted.Column[R]
required: Boolean
我有机会在我的代码中进行这些比较吗?
答案 0 :(得分:1)
在Slick中的filter
,map
等内部,您只能使用可以转换为SQL的代码;很明显,它不能使用像DateTimeComparator
或DateTimeWrapper
这样的任意Java / Scala类。光滑doesn't seem to support datetime comparison currently;你可能想看这个问题。另请参阅Slick: Filtering all records which have a joda DateTime date equal to today。
答案 1 :(得分:0)
这个jodatime映射器对Play非常有用! :https://github.com/tototoshi/slick-joda-mapper