ColdFusion 10 ORM。如何使用2个多对多表自定义EntityLoad返回?

时间:2013-11-19 19:32:34

标签: mysql orm coldfusion hql

我有3张桌子

用户

  • users_pk
  • 的userName

Users_Settings

  • users_settings_pk
  • users_fk
  • settings_fk

设置

  • settings_pk
  • settingName
  • 值列表

用户 Users_Settings 一对多关系 设置 Users_Settings 一对多关系

Users_Settings 用户多对一关系 Users_Settings 设置

多对一关系

我如何使用EntityLoad获取特定用户的所有settings.name和关联的users_settings.value?

我目前的解决方案是进行自定义查询或使用cfquery但是想知道是否有使用coldfusion ORM的方法。

1 个答案:

答案 0 :(得分:1)

假设这些组件及其关系与您的相似(这里只需要最少的代码,不要使用inits(),反转等)...

<强> User.cfc

component persistent="true" table="users"{
    property name="users_pk" fieldType="id" generator="native";
    property name="name" column="userName";
    property name="settings" fieldType="one-to-many" cfc="UserSetting" fkColumn="users_fk";
}

<强> Setting.cfc

component persistent="true" table="settings"{
    property name="settings_pk" fieldType="id" generator="native";
    property name="name" column="settingName";
    property name="userSettings" fieldType=s"one-to-many" cfc="UserSetting" fkColumn="settings_fk";
}

<强> UserSetting.cfc

component persistent="true" table="users_settings"{
    property name="users_settings_pk" fieldType="id" generator="native";
    property name="value";
    property name="user" fieldType="many-to-one" cfc="User" fkColumn="users_fk";
    property name="setting" fieldType="many-to-one" cfc="Setting" fkColumn="settings_fk";
}

...您应该能够加载用户并输出他们的设置,如下所示:

<强> ShowUserSettings.cfm

<cfset user =   EntityLoadByPK( "User",1 )>
<!DOCTYPE html>
<html>
<head>
    <title>User settings</title>
</head>
<body>
    <cfoutput>
        <h2>User #user.getName()#</h2>
        <dl>
            <cfloop array="#user.getSettings()#" index="userSetting">
                <dt>Setting: #userSetting.getSetting().getName()#</dt>
                <dd>Value: #userSetting.getValue()#</dd>
            </cfloop>
        </dl>
    </cfoutput>
</body>
</html>