src/main/scala/fis/crm/model/CrmSchema.scala
changeset 72 077b875a2a0c
parent 69 b1dc0efd1303
child 74 3ce7ecb6bc70
--- a/src/main/scala/fis/crm/model/CrmSchema.scala	Fri Apr 20 08:44:28 2012 +0200
+++ b/src/main/scala/fis/crm/model/CrmSchema.scala	Fri Apr 20 08:44:36 2012 +0200
@@ -55,6 +55,14 @@
     via((c, a) => c.id === a.company)
   companyBankAccounts.foreignKeyDeclaration.constrainReference(onDelete cascade)
 
+  val companyContacts = manyToManyRelation(companyT, contactT).
+    via[CompanyContact]((comp, cnt, cc) => (
+      comp.id === cc.entity,
+      cnt.id  === cc.contact
+    ))
+  companyContacts.leftForeignKeyDeclaration.constrainReference(onDelete cascade)
+  companyContacts.rightForeignKeyDeclaration.constrainReference(onDelete cascade)
+
   def allCompanies: Iterable[Company] = from(companyT) (c =>
     select(c) orderBy(c.name asc))
 
@@ -62,14 +70,24 @@
 
 object CrmSchema extends CrmSchema
 
+object CompanyContacts {
+  def apply(company: Company): Iterable[Contact] =
+    from(CrmSchema.companyContacts.left(company)) (c =>
+      select(c) orderBy(c.lastName asc, c.firstName asc)
+    )
+}
+
 import org.squeryl.KeyedEntity
 import org.squeryl.dsl._
 
-case class EntityContact[T](val entityId: Long, val contactId: Long) extends
-  KeyedEntity[CompositeKey2[Long, Long]] {
+trait EntityContact[T] extends KeyedEntity[CompositeKey2[Long, Long]] {
+  def entity: Long
+  def contact: Long
+  def id = CompositeKey2(entity, contact)
+}
 
-  def id = CompositeKey2(entityId, contactId)
-}
+case class CompanyContact(entity: Long, contact: Long) extends
+  EntityContact[Company]
 
 case class EntityContactWithType[T](val entityId: Long, val contactId: Long,
   val typeId: Long) extends KeyedEntity[CompositeKey3[Long, Long, Long]] {