简体   繁体   English

ColdFusion 9 ORM关系映射

[英]ColdFusion 9 ORM Relationship Mappings

I am trying to define the relationship between tables using ColdFusion 9.0.1 ORM. 我正在尝试使用ColdFusion 9.0.1 ORM定义表之间的关系。

I have 2 tables: users and companies 我有2张桌子:用户和公司

I am working on the relationship between users to companies. 我正在研究用户与公司之间的关系。 Right now I have this: 现在我有这个:

component persistent="true" table="users" datasource="core"
{
    property name="userID" fieldtype="id" column="userID" 
    setter="false" generator="native";

    property name="firstName" length="255";
    property name="lastName" length="255";
    property name="userName" length="45";

    property name="companies" fieldtype="many-to-one" cfc="company" 
    fkcolumn="companyID" singularname="company";

}

and

component persistent="true" table="companies" datasource="core"
{
    property name="companyID" fieldtype="id" column="companyID" 
    setter="false" generator="native";

    property name="companyName" length="255";
    property name="companyCode" length="45";;

    property name="users" fieldtype="one-to-many" fkcolumn="userID" cfc="user"
    cascade="all" inverse="true";


}

I am having an issue with writing to the user table. 我在写入用户表时遇到问题。 I need to write a companyID into the user table so that I can link back to the company. 我需要在用户表中写入一个companyID,以便可以链接回该公司。 On this insert I do not want insert anything into the companies table. 在此插入上,我不想将任何内容插入到company表中。 I just want to write to the users table and have it link to the companies table via a companyID in the users table. 我只想写到用户表,并使其通过用户表中的companyID链接到公司表。

Here is my EntitySave code: 这是我的EntitySave代码:

transaction{

        newUser = EntityNew("user");

            newUser.setCompanies(#FORM.companyID#);     
            newUser.setFirstName(#FORM.firstName#);
            newUser.setLastName(#FORM.lastName#);
            newUser.setUserName(#FORM.userName#);

        EntitySave(newUser);

    }

When I run it like that I get this error "The value for property java.lang.String cannot be retrieved from object of type companyID. Expected object type is company." 当我像这样运行它时,我收到此错误“无法从companyID类型的对象检索属性java.lang.String的值。期望的对象类型为company。”

I was thinking maybe I need to have separate property for the companyID like this "property name="companyID" ormType="int" and then maybe I could use "newUser.setCompanyID(#FORM.companyID#);" in the EntitySave but that does not work. I get an error on the ORMReload(); when I have that in there. 我在想,也许我需要为companyID拥有一个单独的属性,例如“ property name =“ companyID” ormType =“ int”,然后在EntitySave中使用“ newUser.setCompanyID(#FORM.companyID#);”,但是当我在那里有ORMReload()时,我得到一个错误。

I am obviously pretty confused about the best way to setup relationships. 我显然对建立关系的最佳方法感到困惑。 I can get this working without trying to officially reference the relationship in the user.cfc file and just using property name="companyID" ormType="int" but that does not give me a proper relationship between the tables. 我可以在不尝试正式引用user.cfc文件中的关系并且仅使用属性名=“ companyID”或ormType =“ int”的情况下进行此工作,但这并不能为我提供表之间的适当关系。 I think? 我认为?

Any help on this would be great. 任何帮助都会很棒。

Thanks in advance. 提前致谢。

Try changing this: 尝试更改此:

newUser.setCompanies(#FORM.companyID#);     

to this: 对此:

newUser.setCompanies(EntityLoadByPK ("company", FORM.companyID));   

what's with the last "true"? 最后一个“真”是什么? I don't think it is needed unless you use EntityLoad() – 除非您使用EntityLoad(),否则我认为这不是必需的–

EntityLoadByPK ("company", FORM.companyID)

returns an array containing all entities that conform to the criteria (pk=FORM.companyID) 返回一个数组,其中包含符合条件的所有实体(pk=FORM.companyID)

EntityLoadByPK ("company", FORM.companyID, true)

returns an object that conforms to the criteria (pk=FORM.companyID) 返回符合条件的对象(pk=FORM.companyID)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM