我有3张桌子
用户
Users_Settings
设置
用户与 Users_Settings 有一对多关系 设置与 Users_Settings 的一对多关系
Users_Settings 与用户有多对一关系 Users_Settings 与设置
有多对一关系我如何使用EntityLoad获取特定用户的所有settings.name和关联的users_settings.value?
我目前的解决方案是进行自定义查询或使用cfquery但是想知道是否有使用coldfusion ORM的方法。
答案 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>