2012-01-31 49 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

相關問題