如何与 lift/mongodb 创建一对一关系
我正在寻找与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否看过http://www.assembla.com/spaces/liftweb/wiki/Mongo_Record_Referenced_Objects< /a>?
看起来应该使用 2.4-M1 中的 MongoRefField。
Have you seen http://www.assembla.com/spaces/liftweb/wiki/Mongo_Record_Referenced_Objects?
Looks like one should use MongoRefField from 2.4-M1.