【问题标题】:Using Model to establish inner join between tables使用 Model 建立表之间的内连接
【发布时间】:2015-02-17 13:49:31
【问题描述】:

我正在尝试在 articlescategories 这两个表之间建立关系。 articles.category_id = categories.id 是一对一的关系。我有以下设置。

controllers/home.cfc

<cfcomponent extends="Controller">

    <cffunction name="index">

        <cfset qFeaturedArticles = model("articles").findAll(
            where="show_homepage = 1",
            include="categories",
            order="homepage_order"
        ) />

    </cffunction>

</cfcomponent>

model/categories.cfc

<cfcomponent extends="Model">

    <cffunction name="init">
        <cfset hasOne("articles", foreignKey="category_id") />
    </cffunction>

</cfcomponent>

model/articles.cfc

<cfcomponent extends="Model">

   <cffunction name="init">
    <cfset belongsTo("categories", dependent="nullify") />
</cffunction>

</cfcomponent>

这是我遇到的错误。

在第 4 行第 49 列发现无效的 CFML 构造。ColdFusion 正在查看以下文本:

=

CFML 编译器正在处理:

  • 以belongsTo 开头的表达式,第4 行第16 列。此消息通常是由表达式结构中的问题引起的。
  • 从第 4 行第 10 列开始的 cfset 标记。
  • 从第 4 行第 10 列开始的 cfset 标记。
包含或处理的文件的具体顺序为:\cfusion\wwwroot\foo\index.cfm, line: 4

【问题讨论】:

  • index.cfm 的第 4 行是什么?
  • wheels/index.cfm 的cfinclude;即没有在我端被操纵的代码。

标签: mysql coldfusion cfwheels


【解决方案1】:

首先我要指出categories.cfc 模型。如果您指定hasOne 关系,那么在这种情况下,代码应如下所示:

<cfset hasOne("article", foreignKey="category_id") />

请注意,如果hasOne,您需要输入article,而不是articles

但是,从字面上看,分类和文章的关系应该是one to many。我的意思是一个类别可以有很多文章。

所以categories.cfc应该写成如下:

<cfcomponent extends="Model">

<cffunction name="init">
    <cfset hasMany("articles", foreignKey="category_id") />
</cffunction>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2015-02-09
    相关资源
    最近更新 更多