如何与 lift/mongodb 创建一对一关系

发布于 2024-11-15 22:32:36 字数 2181 浏览 0 评论 0原文

我正在寻找与 lift/mongodb 一对一关系的简单示例。 我可以创建一个像这样的简单 MongoRecord :

class Blob extends MongoRecord[Blob] with MongoId[Blob] {
  def meta = Blob

  object name extends StringField[Blob](this,10)

}
object Blob extends Blob with MongoMetaRecord[Blob]{

}

现在我想将父/子关系添加到 Blob 中,我该怎么做?

编辑:

我终于找到了如何解决我的问题(这个指南 给我很多帮助)。 这是结果:

MongoRecord :

package code.model

import net.liftweb.mongodb.record.{MongoMetaRecord, MongoRecord}
import net.liftweb.record.field.StringField
import net.liftweb.mongodb.record.field.{ObjectIdRefField, ObjectIdPk}


class Blob extends MongoRecord[Blob] with ObjectIdPk[Blob]{

  def meta = Blob

  object name extends StringField[Blob](this,10)

  object child extends ObjectIdRefField(this,Blob)

}

object Blob extends Blob with MongoMetaRecord[Blob]

这就是我创建和读取记录的方式:

package code.lib

import net.liftweb.http._
import code.model.Blob
import net.liftweb.common.{Full}

object REST {
  lazy val blob: LiftRules.DispatchPF = {
    case Req("api" :: "blob" :: Nil, suffix, GetRequest) => {
      () => {
        val blobs = Blob.findAll.map(blob => {

          val child = Blob.find(blob.child.is)
          val childName = if (child.isDefined) child.open_!.name else "?"

          <blob>
            <id>{blob.id.is}</id>
            <name>{blob.name}</name>
            <child_name>{childName}</child_name>
          </blob>
        })

        Full(XmlResponse(<blobs>{blobs}</blobs>))
      }
    }
    case Req("api" :: "blob" :: Nil, suffix, PostRequest) => {
      () => {
        val randID = (scala.math.random * 999).toInt

        val child = Blob.createRecord
        child.name("child_" + randID)
        child.save

        val blob = Blob.createRecord
        blob.name("blob_" + randID)
        blob.child(child.id.is)
        blob.save

        Full(XmlResponse(<result>{blob.id.is}</result>))
      }
    }
  }
}

我仍然不确定这是否是正确的方法,所以: 欢迎任何意见!

I'm looking for a simple example of a one to one relation with lift/mongodb.
I'm able to create a simple MongoRecord like this one :

class Blob extends MongoRecord[Blob] with MongoId[Blob] {
  def meta = Blob

  object name extends StringField[Blob](this,10)

}
object Blob extends Blob with MongoMetaRecord[Blob]{

}

Now i would like to add parent/child relation into Blob, how could i do that ?

EDIT :

I finaly found how to solve my problem (this guide help me a lot).
Here is the result :

MongoRecord :

package code.model

import net.liftweb.mongodb.record.{MongoMetaRecord, MongoRecord}
import net.liftweb.record.field.StringField
import net.liftweb.mongodb.record.field.{ObjectIdRefField, ObjectIdPk}


class Blob extends MongoRecord[Blob] with ObjectIdPk[Blob]{

  def meta = Blob

  object name extends StringField[Blob](this,10)

  object child extends ObjectIdRefField(this,Blob)

}

object Blob extends Blob with MongoMetaRecord[Blob]

This is how i create and read records :

package code.lib

import net.liftweb.http._
import code.model.Blob
import net.liftweb.common.{Full}

object REST {
  lazy val blob: LiftRules.DispatchPF = {
    case Req("api" :: "blob" :: Nil, suffix, GetRequest) => {
      () => {
        val blobs = Blob.findAll.map(blob => {

          val child = Blob.find(blob.child.is)
          val childName = if (child.isDefined) child.open_!.name else "?"

          <blob>
            <id>{blob.id.is}</id>
            <name>{blob.name}</name>
            <child_name>{childName}</child_name>
          </blob>
        })

        Full(XmlResponse(<blobs>{blobs}</blobs>))
      }
    }
    case Req("api" :: "blob" :: Nil, suffix, PostRequest) => {
      () => {
        val randID = (scala.math.random * 999).toInt

        val child = Blob.createRecord
        child.name("child_" + randID)
        child.save

        val blob = Blob.createRecord
        blob.name("blob_" + randID)
        blob.child(child.id.is)
        blob.save

        Full(XmlResponse(<result>{blob.id.is}</result>))
      }
    }
  }
}

I'am still not sure if it's the correct way to do, so :
any comments are welcome !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文