Permalink
Browse files

mapper: 2.10: fix {One,Many}ToMany

  • Loading branch information...
1 parent d02b61d commit 4f1b19dc669207299cf94912fdb718ac082e4199 @nafg nafg committed Sep 19, 2012
@@ -71,9 +71,6 @@ trait ManyToMany extends BaseKeyedMapper {
val otherMeta: MetaMapper[T2],
val qp: QueryParam[O]*) extends scala.collection.mutable.Buffer[T2] {
- def thisFK[A](join: O)(f: MappedForeignKey[K,O,_>:T] => A): A =
- thisField.actualField(join) match { case mfk: MappedForeignKey[K,O,T] => f(mfk) }
-
def otherFK[A](join: O)(f: MappedForeignKey[K2,O,T2] => A): A =
otherField.actualField(join) match { case mfk: MappedForeignKey[K2,O,T2] => f(mfk) }
@@ -106,7 +103,7 @@ trait ManyToMany extends BaseKeyedMapper {
removedJoin // well, noLongerRemovedJoin...
case None =>
val newJoin = joinMeta.create
- thisFK(newJoin)(_.apply(ManyToMany.this))
+ thisField.actualField(newJoin).asInstanceOf[MappedForeignKey[K,O,T]] apply ManyToMany.this.primaryKeyField.is.asInstanceOf[K]
otherFK(newJoin)(_.apply(e))
newJoin
}
@@ -120,7 +117,7 @@ trait ManyToMany extends BaseKeyedMapper {
removedJoins = join :: removedJoins
val o = otherField.actualField(join)
o.set(o.defaultValue)
- thisFK(join)(f => f.set(f.defaultValue))
+ thisField.actualField(join) match { case mfk => mfk set mfk.defaultValue }
Some(join)
case None =>
None
@@ -190,7 +187,7 @@ trait ManyToMany extends BaseKeyedMapper {
* Discard the cached state of this MappedManyToMany's children and reinitialize it from the database
*/
def refresh = {
- val by = new Cmp[O, TheKeyType](thisField, OprEnum.Eql, Full(primaryKeyField.is), Empty, Empty)
+ val by = new Cmp[O, TheKeyType](thisField, OprEnum.Eql, Full(primaryKeyField.is.asInstanceOf[K]), Empty, Empty)
_joins = joinMeta.findAll( (by :: qp.toList): _*)
all
@@ -210,7 +207,9 @@ trait ManyToMany extends BaseKeyedMapper {
_joins = joins.filter { join =>
otherFK(join)(f => f.is != f.defaultValue)
}
- _joins foreach { thisFK(_)(_ set ManyToMany.this.primaryKeyField.is) }
+ _joins foreach {
+ thisField.actualField(_).asInstanceOf[MappedForeignKey[K,O,X] forSome {type X <: KeyedMapper[K,X]}] set ManyToMany.this.primaryKeyField.is.asInstanceOf[K]
+ }
removedJoins.forall {_.delete_!} & ( // continue saving even if deleting fails
children.forall(_.save) &&
@@ -26,13 +26,13 @@ package mapper
* @author nafg
*/
trait OneToMany[K,T<:KeyedMapper[K, T]] extends KeyedMapper[K,T] { this: T =>
- private[mapper] lazy val oneToManyFields: List[MappedOneToManyBase[_]] = {
- new FieldFinder[MappedOneToManyBase[_]](
+ private[mapper] lazy val oneToManyFields: List[MappedOneToManyBase[_ <: Mapper[_]]] = {
+ new FieldFinder[MappedOneToManyBase[_ <: Mapper[_]]](
getSingleton,
net.liftweb.common.Logger(classOf[OneToMany[K,T]])
- ).accessorMethods map (_.invoke(this).asInstanceOf[MappedOneToManyBase[_]])
+ ).accessorMethods map (_.invoke(this).asInstanceOf[MappedOneToManyBase[_ <: Mapper[_]]])
}
-
+
/**
* An override for save to propagate the save to all children
* of this parent.
@@ -52,9 +52,10 @@ trait OneToMany[K,T<:KeyedMapper[K, T]] extends KeyedMapper[K,T] { this: T =>
* If they are all successful returns true.
*/
override def delete_! = DB.use(connectionIdentifier){_ =>
- if(oneToManyFields.forall{
- case f: Cascade[_] => f.delete_!
- case _ => true
+ if(oneToManyFields.forall{(_: MappedOneToManyBase[_ <: Mapper[_]]) match {
+ case f: Cascade[_] => f.delete_!
+ case _ => true
+ }
})
super.delete_!
else {
@@ -97,7 +98,7 @@ trait OneToMany[K,T<:KeyedMapper[K, T]] extends KeyedMapper[K,T] { this: T =>
* @param foreign A function that gets the MappedForeignKey on the child that refers to this parent
*/
class MappedOneToManyBase[O <: Mapper[_]](val reloadFunc: ()=>Seq[O],
- val foreign: O => MappedForeignKey[K,_,T]) extends scala.collection.mutable.Buffer[O] {
+ val foreign: O => MappedForeignKey[K,X,T] forSome { type X <: Mapper[X] }) extends scala.collection.mutable.Buffer[O] {
private var inited = false
private var _delegate: List[O] = _
/**
@@ -118,7 +119,7 @@ trait OneToMany[K,T<:KeyedMapper[K, T]] extends KeyedMapper[K,T] { this: T =>
*/
protected def own(e: O) = {
foreign(e) match {
- case f: MappedLongForeignKey[O,T] with MappedForeignKey[_,_,T] =>
+ case f: MappedLongForeignKey[O,T] with MappedForeignKey[K,_,T] =>
f.apply(OneToMany.this)
case f =>
f.set(OneToMany.this.primaryKeyField.get)

0 comments on commit 4f1b19d

Please sign in to comment.