2012-01-31 75 views
2

首先,表结构的相关位:这种关系在CF9的ORM中可能吗?怎么样?

contact 
    -contactID 
    -email 

data 
    -value 
    -contactID 
    -definitionID 

definition 
    -definitionID 
    -name 

contact表中的每个记录代表一个有自己的E-mail地址沿着单一的接触。

definition表中的每条记录表示自定义数据字段的定义。例如,definition中可能有五条记录 - 组织,邮编,评论,地址,电话。除了这些字段的名称之外,它还定义了有关这些字段的相关元数据。

data表中的每条记录都包含与联系人相关的自定义数据字段的值。其定义取自definition表。

为了进一步澄清,如果我想与他们的自定义字段数据一起产生接触的表,它可能是这样的:

| E-Mail   | Organization | Zip Code | Comments       | Address  | Phone | 
------------------------------------------------------------------------------------------------------------- 
| [email protected] | ACME   | 12345 | Cool guy!      | 123 Test St | 555-5555 | 
| [email protected] | SomeCo.  | 54321 | Doesn't know anything about ORM! | 321 Test Blvd | 444-4444 | 

该系统的好处是,它可以扩展为多因为我需要它并且很容易定制。不利的是,我不知道如何界定的关系:)

我已经尝试定义contact:data1:Mdefinition:data1:M,但结果看起来有点奇怪:有两个触点和单一的定义,添加对每个联系人排列数据,然后调用entityLoad('Contact')产生一个有趣的关系。它看起来像这样(只是要使用一些伪结构符号,因为它很容易输入,而且我希望,读):

{ 
    contact: { 
     email: '[email protected]', 
     data: { 
      value: 'ACME', 
      definition: { 
       name: 'Organization', 
       data: { 
        value: 'SomeCo.', 
        contact: { 
         email: '[email protected]' 
       } 
      } 
     } 
    } 
} 

它看起来就像是创造contactdefinition之间的间接关系,根据data表与两个表的关系。正如你所想象的,增加联系人和自定义字段的数量只会使问题呈指数级恶化。

这种类型的关系可能使用CF9的ORM吗?我怎样才能做到这一点?

在此先感谢!

编辑:忘了指定 - 我使用MySQL,如果它很重要。

编辑2:CFC定义遵循:

Contact.cfc

/** 
* @persistent true 
*/ 
component name='Contact' { 

    /** 
    * @type numeric 
    * @sqltype int(11) 
    * @generator increment 
    * @fieldtype id 
    */ 
    property contactID; 

    /** 
    * @type string 
    * @sqltype varchar(50) 
    */ 
    property email; 

    /** 
    * @type array 
    * @fieldtype one-to-many 
    * @cfc Data 
    * @fkcolumn dataID 
    */ 
    property data; 
} 

Definition.cfc

/** 
* @persistent true 
*/ 
component name='Definition' { 

    /** 
    * @type numeric 
    * @sqltype int(11) 
    * @generator increment 
    * @fieldtype id 
    */ 
    property definitionID; 

    /** 
    * @type string 
    * @sqltype varchar(50) 
    */ 
    property name; 

    /** 
    * @type array 
    * @fieldtype one-to-many 
    * @cfc Data 
    * @fkcolumn dataID 
    */ 
    property data; 

} 

Data.cfc

/** 
* @persistent true 
*/ 
component { 

    /** 
    * @type numeric 
    * @sqltype int(11) 
    * @generator increment 
    * @fieldtype id 
    */ 
    property dataID; 

    /** 
    * @type string 
    * @sqltype varchar(50) 
    */ 
    property value; 

    /** 
    * @fieldtype many-to-one 
    * @fkcolumn contactID 
    * @cfc Contact 
    * @inverse true 
    */ 
    property contact; 

    /** 
    * @fieldtype many-to-one 
    * @fkcolumn definitionID 
    * @cfc Definition 
    * @inverse true 
    */ 
    property definition; 

} 
+1

我相信你想要实现的数据属性一个多到多的接触和定义与数据作为你的“LinkTable”之间的关系:HTTP: //help.adobe.com/zh_CN/ColdFusion/9.0/Developing/WS5FFD2854-7F18-43ea-B383-161E007CE0D1.html – 2012-01-31 22:11:40

+0

第二个想法,我不知道你将如何访问“数据”中的“值”字段,如果你用它作为多对多的链接表。也许正确的做法是定义联​​系人和数据之间的一对多关系,然后在数据和定义之间建立多对一的关系。 – 2012-01-31 22:19:05

+0

@ Danimal37感谢您的意见!正如我的问题所指出的那样,我将'definition:data'和'contact:data'定义为一对多,并且得到了上面的结果:) – 2012-01-31 22:44:32

回答

1

我相信你要的关系是由数据处理。CFC,具有

Data --m2o--> Contact 
Data --m2o--> Definition 

并且为了方便

Contact --o2m--> Data (inverse=true) 

CFC的

// Contact.cfc 
component persistent="true" { 
    property name="contactID" fieldtype="id" generator="native"; 
    property name="email"; 
    property name="data" cfc="Data" fieldtype="one-to-many" inverse="true" lazy="true"; 
} 

// Definition.cfc 
component persistent="true" { 
    property name="definitionID" fieldtype="id" generator="native"; 
    property name="name"; 
} 

// Data.cfc 
component persistent='true'{ 
    property name="dataID" fieldtype="id" generator="native"; 
    property name="value"; 
    property name="contact" cfc="Contact" fieldtype="many-to-one" fkcolumn="contactID"; 
    property name="definition" cfc="Definition" fieldtype="many-to-one" fkcolumn="definitionID"; 
} 

index.cfm

<cfscript> 
ormReload(); 
transaction { 
    con = new Contact(); 
    con.setEmail("[email protected]"); 
    entitySave(con); 

    def1 = new Definition(); 
    def1.setName("twitter"); 
    entitySave(def1); 

    def2 = new Definition(); 
    def2.setName("interests"); 
    entitySave(def2); 

    data1 = new Data(); 
    data1.setValue("d1rtym0nk3y"); 
    data1.setDefinition(def1); 
    data1.setContact(con); 
    entitySave(data1); 

    data2 = new Data(); 
    data2.setValue("ColdFusion"); 
    data2.setDefinition(def2); 
    data2.setContact(con); 
    entitySave(data2); 

    // this is important, you must set both sides of the relationship or "bad things" happen 
    // i'd recommend overriding contact.addData()/data.setContact to ensure both sides get set 
    con.addData(data1); 
    con.addData(data2); 
} 
writeDump(con); 
</cfscript> 

要检索以一种合理的方式联系人,你可以做

myData = ormExecuteQuery(" 
    select new map(
     data.value as value, 
     def.name as name 
    ) 
    from Data data 
    join data.definition def 
    where data.contact.contactID = :id 

", {id=con.getContactID()}); 

writeDump(myData); 
+0

钉了它,谢谢! :)我的印象是这种关系必须由双方定义,这可能是我在这种情况下的失败。 – 2012-02-07 22:05:27

相关问题