如何使用使用字符串主键而不是长主键的自定义特征替换特征IdPK?

时间:2014-09-14 16:58:09

标签: scala lift traits

另一个特质问题: - (

我将IdPK特性混合到我的Lift Mapper模型中,如下所示:

class Test1 extends LongKeyedMapper[Test1] with IdPK {

我想切换到使用String索引而不是Long索引(我现在不想,但我必须这样做)。我正在尝试编写特征StringIdPK来替换IdPK特征。这是我的尝试:

trait StringIdPK /* extends BaseLongKeyedMapper */ {  
  self: KeyedMapper[String] =>  
  def primaryKeyField = id  
  object id extends MappedStringIndex(this, 50)  
}

(我的基础是IdPK特征,如下所示:

trait IdPK /* extends BaseLongKeyedMapper */ {  
  self: BaseLongKeyedMapper =>  
  def primaryKeyField = id  
  object id extends MappedLongIndex[MapperType](this.asInstanceOf[MapperType])  
} 

可以找到此特征的代码here

当我尝试编译时,出现以下错误:

wrong number of type arguments for net.liftweb.mapper.KeyedMapper, should be 2

KeyedMapper的规范(可以找到here)是:

trait KeyedMapper[KeyType, OwnerType <: KeyedMapper[KeyType, OwnerType]] extends Mapper[OwnerType] with BaseKeyedMapper

我需要更好地理解OwnerType <: KeyedMapper[KeyType, OwnerType]的含义以及如何在我的自定义特征中容纳它。所以我的问题是:在我对上述特征StringIdPK的定义中,我做错了什么? (这有助于我了解如何处理OwnerType <: KeyedMapper[KeyType, OwnerType])。

谢谢!

更新:我认为以下内容接近解决它(见下文 - 我从here获取了一些代码)。它仍然不太正确,因为编译器仍然抱怨我扩展的FK对象没有定义外部元。

trait BaseStringKeyedMapper extends BaseKeyedMapper{
    override type TheKeyType = String
}

trait StringKeyedMapper[OwnerType <: StringKeyedMapper[OwnerType]] extends KeyedMapper[String, OwnerType] with BaseStringKeyedMapper {
    self: OwnerType =>
}


trait StringKeyedMetaMapper[A <: StringKeyedMapper[A]] extends KeyedMetaMapper[String, A] {
    self: A =>
}

trait StringIdPK {  
  self: BaseStringKeyedMapper =>  
  def primaryKeyField = id  
  object id extends MappedStringIndex[MapperType](this.asInstanceOf[MapperType], 50)  
} 

此外,还有关于此问题的帖子on the lift google group here,但似乎尚未解决。

1 个答案:

答案 0 :(得分:0)

我认为以下内容解决了这个问题(我现在无法确认,因为在我真正测试它之前我正在解决另一个问题,但到目前为止这似乎有效。我从{{3)获取了一些代码我和here。当我了解有关此实现的更多信息时,我会更新此信息:

trait BaseStringKeyedMapper extends BaseKeyedMapper{
    override type TheKeyType = String
}

trait StringKeyedMapper[OwnerType <: StringKeyedMapper[OwnerType]] extends KeyedMapper[String, OwnerType] with BaseStringKeyedMapper {
    self: OwnerType =>
}

trait StringKeyedMetaMapper[A <: StringKeyedMapper[A]] extends KeyedMetaMapper[String, A] {
    self: A =>
}

trait StringIdPK {  
  self: BaseStringKeyedMapper =>  
  def primaryKeyField = id  
  object id extends MappedStringIndex[MapperType](this.asInstanceOf[MapperType], 50) {
      override def writePermission_? = true // if u want to set it via your code, keep this true
      override def dbAutogenerated_? = false
      override def dbNotNull_? = true

      // override def writePermission_? = false
      override def readPermission_? = true
      override def shouldDisplay_? = true
      override def show_? = true
      override def dbDisplay_? = true
      override def validations = valUnique(S.?("Must be unique")) _ :: super.validations
  }
} 

我在id字段中添加了一些覆盖,以便将其与CRUDify混合使用。由于某种原因,我无法弄清楚,valUnique不起作用(其他验证,例如valMinLen确实有效)。我将发布一个关于此的新问题。