我正在尝试在文章和类别这两个表之间建立关系。这是一对一的关系,其中 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 编译器正在处理:
- 第 4 行第 16 列上以 ownTo 开头的表达式。此消息通常是由以下问题引起的:表达式结构。
- 从第 4 行第 10 列开始的 cfset 标记。
- 从第 4 行开始的 cfset 标记,第10列。
最佳答案
首先我想指出categories.cfc模型。如果您指定 hasOne
关系,那么在这种情况下,代码应如下所示:
<cfset hasOne("article", foreignKey="category_id") />
请注意,在 hasOne
的情况下,您需要放置 article
,而不是 articles
。
但是,从字面上来说,类别和文章之间的关系应该是一对多
。我的意思是一个类别可以有很多文章。
所以categories.cfc应该写成如下:
<cfcomponent extends="Model">
<cffunction name="init">
<cfset hasMany("articles", foreignKey="category_id") />
</cffunction>
关于mysql - 使用Model建立表之间的内连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28554061/