我有以下案例类
case class BusinessInput(userId: String, name: String, location: Point, address: Option[String], phonenumber: Option[String], email: Option[String], hours: Seq[BusinessHoursInput])
以下BusinessHours案例类:
case class BusinessHoursInput(weekDay: Int, startTime: Time, endTime: Time)
我正在使用Slick,我正在尝试将BusinessHours行添加到BusinessInput的hours字段中的每个BusinessHoursinput对象的DB中。我尝试过以下方法:
_ <- for(businessHours <- businessInput.hours) BusinessHours += BusinessHoursRow(businessRow.businessId, businessHours.weekDay, businessHours.startTime, businessHours.endTime)
但是我坚持以下错误:
value map is not a member of Unit
[error] _ <- for(businessHours <- businessInput.hours) BusinessHours += BusinessHoursRow(businessRow.businessId, businessHours.weekDay, businessHours.startTime, businessHours.endTime )
[error] ^
编辑:
这是我的Repo的创建功能,我首先尝试插入BusinessRow对象,然后从该插入中获取自动增量的Id,并使用先前插入的业务对象中的Business Id插入businessHours对象列表。然后我想返回我第一次插入的业务对象:
def create(businessInput: BusinessInput): Future[BusinessRow] = db.run(for {
// We create a projection of just the Business Columns, since we're not inserting a value for the id column
businessRow <- (Business.map(b => (b.userId, b.name, b.location, b.address, b.phonenumber, b.email))
// Now define it to return the id, because we want to know what id was generated for the Business
returning Business.map(_.businessId)
// And we define a transformation for the returned value, which combines our original parameters with the
// returned id
into((userIdAndBusiness, id) => BusinessRow(id, userIdAndBusiness._1, userIdAndBusiness._2, userIdAndBusiness._3, userIdAndBusiness._4, userIdAndBusiness._5, userIdAndBusiness._6))
// And finally, insert the business into the database
) +=(businessInput.userId, businessInput.name, businessInput.location, businessInput.address, businessInput.phonenumber, businessInput.email)
// Insert a sequence of BusinessHours objects
_ = for(businessHours <- businessInput.hours) BusinessHours += BusinessHoursRow(businessRow.businessId, businessHours.weekDay, businessHours.startTime, businessHours.endTime)
} yield (businessRow)
)
以下是打印小时字段的内容:
hours -> Vector(ListMap(weekDay -> 1, startTime -> 07:00, endTime -> 10:00), ListMap(weekDay -> 2, startTime -> 07:00, endTime -> 10:00))
现在我收到以下错误:
scala.collection.immutable.ListMap$Node cannot be cast to dao.BusinessHoursInput
答案 0 :(得分:1)
如果此行在for-yield理解中,则需要将第一个<-
更改为=
。由于BusinessHours += ...
会返回Unit
,_ <-
会被flatMap
或map
删除。
或者,您可以将BusinessHours += ...
放入yield
。
答案 1 :(得分:1)
问题是你正在使用被翻译成map
的理解。地图需要将hours: Seq[BusinessHoursInput]
转换为其他内容。
在地图内你这样做:BusinessHours += BusinessHoursRow(businessRow.businessId, businessHours.weekDay, businessHours.startTime, businessHours.endTime)
此选项不会将hours
转换为其他内容。它在BusinessHoursRow
中添加BusinessHours
,该操作返回Unit。
那么如何解决呢?
1)将BusinessHoursInput
转换为BusinessHoursRow
,然后将其插入BusinessHours
:
var businessHoursRows = businessInput.hours.map { businessHoursInput =>
BusinessHoursRow(businessHoursInput.businessId, businessHoursInput.weekDay,
businessHoursInput.startTime, businessHoursInput.endTime)
}
BusinessHours ++= businessHoursRows
2)使用foreach
代替map
:
businessInput.hours.foreach { businessHoursInput =>
businessHoursRows += BusinessHoursRow(businessHoursInput.businessId, businessHoursInput.weekDay,
businessHoursInput.startTime, businessHoursInput.endTime)
}