Created
June 18, 2012 18:32
-
-
Save rktoomey/2949881 to your computer and use it in GitHub Desktop.
Salat parent child relationship
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package model | |
import org.joda.time.DateTime | |
import org.bson.types.ObjectId | |
import com.novus.salat.annotations._ | |
import com.novus.salat.dao.SalatDAO | |
import com.mongodb.casbah.Imports._ | |
object Section { | |
def subSections(s: Section) = SectionDAO.subSections(s.id) | |
} | |
case class Section(@Key("_id") id: ObjectId = new ObjectId, | |
name: String) | |
object SubSection { | |
def questions(s: SubSection) = QuestionDAO.bySubSection(s.id) | |
} | |
case class SubSection(@Key("_id") id: ObjectId = new ObjectId, | |
sectionId: ObjectId, | |
name: String) | |
case class Question(@Key("_id") id: ObjectId = new ObjectId, | |
subSectionId: ObjectId) | |
object SectionDAO extends SalatDAO[Section, ObjectId](collection = MongoConnection()("salat_db")("sections")) { | |
val subSectionColl = new ChildCollection[SubSection, ObjectId](collection = MongoConnection()("salat_db")("subsections"), | |
parentIdField = "sectionId") { | |
def questions(subSectionId: ObjectId) = QuestionDAO.bySubSection(subSectionId).toList | |
} | |
def subSections(sectionId: ObjectId) = subSectionColl.findByParentId(sectionId).toList | |
} | |
object QuestionDAO extends SalatDAO[Question, ObjectId](collection = MongoConnection()("salat_db")("questions")) { | |
def bySubSection(subSectionId: ObjectId) = find(MongoDBObject("subSectionId" -> subSectionId)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment