[英]Using Model to establish inner join between tables
我正在尝试在两个表, 文章和类别之间建立关系。 这是一对一的关系,其中articles.category_id = categories.id
。 我有以下设置。
控制器/ home.cfc
<cfcomponent extends="Controller">
<cffunction name="index">
<cfset qFeaturedArticles = model("articles").findAll(
where="show_homepage = 1",
include="categories",
order="homepage_order"
) />
</cffunction>
</cfcomponent>
模型/ categories.cfc
<cfcomponent extends="Model">
<cffunction name="init">
<cfset hasOne("articles", foreignKey="category_id") />
</cffunction>
</cfcomponent>
模型/ articles.cfc
<cfcomponent extends="Model">
<cffunction name="init">
<cfset belongsTo("categories", dependent="nullify") />
</cffunction>
</cfcomponent>
这是我得到的错误。
在第4行第49列中发现无效的CFML构造。ColdFusion正在查看以下文本:
=
CFML编译器正在处理:
首先,我要指向category.cfc模型。 如果您指定hasOne
关系,那么在这种情况下,代码应如下所示:
<cfset hasOne("article", foreignKey="category_id") />
请注意,而不是articles
,你需要把article
中的情况下hasOne
。
但是,从字面上来讲,类别和文章之间的关系应该是one to many
。 我的意思是一个类别可以有很多文章。
因此,category.cfc应该写为:
<cfcomponent extends="Model">
<cffunction name="init">
<cfset hasMany("articles", foreignKey="category_id") />
</cffunction>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.