我遇到了与 menu.cfc 和 menuItem.cfc 的多对一/一对多关系的问题。它们都扩展了 node.cfc (最后声明了这些)。
索引.cfm
<cfset oMenu = entityNew( "menu" )>
<cfset oMenu.setTitle( "Main menu" )>
<cfset entitySave( oMenu )>
<cfset ORMFlush()>
<cfset oMenuItem = entityNew( "menuItem" )>
<cfset oMenuItem.setTitle( "First main menu item" )>
<cfset oMenuItem.setMenu( oMenu )>
<cfset entitySave( oMenuItem )>
<cfset ORMFlush()>
<cfdump var="#oMenu.getMenuItems()#">
<!--- array[empty] --->
<!--- WHY ?--->
在数据库中,我的 menu_item.menu_id 是正确的。
我也试过
<cfset oMenu = entityNew( "menu" )>
<cfset oMenu.setTitle( "Main menu" )>
<cfset entitySave( oMenu )>
<cfset ORMFlush()>
<cfset oMenuItem = entityNew( "menuItem" )>
<cfset oMenuItem.setTitle( "First main menu item" )>
<cfset entitySave( oMenuItem )>
<cfset ORMFlush()>
<cfset oMenu.addMenuItem( oMenuItem )>
<cfdump var="#oMenu.getMenuItems()#">
<!--- array[empty] --->
在数据库中,我的 menu_item.menu_id 为空!
如果您知道我做错了什么,那就太好了。谢谢!
节点.cfc
<cfcomponent entityname="node" output="false" persistent="true" table="nodes" discriminatorcolumn="type">
<cfproperty name="id" fieldtype="id" generator="identity" default="0" unsavedvalue="0">
<cfproperty name="type" insert="false" update="false">
<cfproperty name="title" ormtype="string" notnull="true" default="" loadInForm="true" required="true">
<cfproperty name="systemName" column="system_name" ormtype="string" notnull="true" default="" loadInForm="true" uniquekey="system_name_lang">
<cfproperty name="parents" singularname="parent" fieldtype="one-to-many" cfc="nodeHierarchy" inverse="true" weight="weight" clearBeforeSave="true">
<cfproperty name="children" singularname="child" fieldtype="one-to-many" cfc="nodeHierarchy" inverse="true" orderby="weight asc">
<cfproperty name="node" fieldtype="many-to-one" cfc="node" fkcolumn="nid">
<cfproperty name="created" ormtype="timestamp" sqltype="timestamp">
<cfproperty name="updated" ormtype="timestamp" sqltype="timestamp">
</cfcomponent>
菜单.cfc
<cfcomponent entityname="menu" output="false" persistent="true" table="menus" extends="node" joincolumn="node_id" discriminatorvalue="menu">
<cfproperty name="menuItems" singularname="menuItem" fieldtype="one-to-many" cfc="menuItem" inverse="true">
</cfcomponent>
菜单项.cfc
<cfcomponent entityname="menuItem" output="false" persistent="true" table="menus_items" extends="node" joincolumn="node_id" discriminatorvalue="menu_item">
<cfproperty name="menu" fieldtype="many-to-one" cfc="menu" fkcolumn="menu_id">
<cfproperty name="parentMenuItem" fieldtype="many-to-one" cfc="menuItem" fkcolumn="menu_item_id">
<cfproperty name="childMenuItems" fieldtype="one-to-many" cfc="menuItem" fkcolumn="menu_item_id">
<cfproperty name="node" fieldtype="one-to-one" cfc="com.cfc.app.models.node" fkcolumn="nid">
</cfcomponent>