【问题标题】:CFWheels - Calculated property generates column error in queryCFWheels - 计算属性在查询中生成列错误
【发布时间】:2012-08-03 23:12:27
【问题描述】:

好的,所以现在这真的让我很困惑,因为我之前有过这个工作,但发生了一些变化,导致我的计算属性不起作用。

我有一个“页面”控制器,它在“链接”模型上执行 findAll()。这很好用,除非我尝试包含一个计算属性(它曾经可以工作)。

page.cfc(控制器)

<cfset linkListHottest = model("link").findAll(

            select="

                links.linkID,
                linkTitle,
                linkPoints,
                linkAuthority,
                linkCreated,
<!--- Here I specifiy the 'property' names from the model --->
                linkUpVoteCount,
                linkDownVoteCount,
                linkCommentCount,

                users.userID,
                userName,

                categories.categoryID,
                categoryTitle,
                categoryToken",

            include="user,category", 
            order="linkPoints DESC",
            handle="linkListHottestPaging",

            page=params.page,
            perPage=5

        ) />

link.cfc(模型)

<cffunction name="init">

        <cfset table("links") />

<!--- These three lines aren't populating my queries on the link model --->
        <cfset property(name="linkUpVoteCount", sql="(SELECT COUNT(*) FROM votes WHERE votes.linkID = links.linkID AND voteType = 1)") />
        <cfset property(name="linkDownVoteCount", sql="(SELECT COUNT(*) FROM votes WHERE votes.linkID = links.linkID AND voteType = 0)") />
        <cfset property(name="linkCommentCount", sql="(SELECT COUNT(*) FROM comments WHERE comments.linkID = links.linkID AND commentRemoved = 0)") />

        <cfset belongsTo(name="user", modelName="user", joinType="outer", foreignKey="userID") />
        <cfset belongsTo(name="category", modelName="category", joinType="outer", foreignKey="categoryID") />

        <cfset hasMany(name="vote", modelName="vote", joinType="outer", foreignKey="linkID") />
        <cfset hasMany(name="comment", modelName="comment", joinType="outer", foreignKey="linkID") />   

        <cfset validatesPresenceOf(property='linkTitle') />
        <cfset validatesPresenceOf(property='linkURL') />
        <cfset validate(property='linkURL', method='isValidURL') />
        <cfset validate(property='linkURL', method="validateUniqueUrl", when="onCreate") />

    </cffunction>

home.cfm(查看)

#linkListHottest.linkUpVoteCount#

我收到以下错误:

“字段列表”中的未知列“linkUpVoteCount”

好的,我想,让我们从 findAll() 中的 SELECT 中删除列名,看看是否能解决问题。没有:

在查询中找不到列 [LINKUPVOTECOUNT],列是 [linkID,linkTitle,linkPoints,linkAuthority,linkCreated,userID,userName,categoryID,categoryTitle,categoryToken]

所以这似乎是第 22 条问题。就好像我的“链接”模型完全忽略了那里设置的属性。

如果有任何关于我哪里出错的反馈,我将不胜感激(我确定是我自己!)。

非常感谢, 迈克尔。

【问题讨论】:

  • 如果只删除linkUpVoteCount,列linkDownVoteCount是否也会出现同样的错误?

标签: sql coldfusion railo cfwheels


【解决方案1】:

听起来这是 Wheels 的一个错误。我建议提交一个问题。

【讨论】:

  • 嗨,克里斯,是的,我在 GitHub 上为 CFWheels 放置了一个“问题”……这是正确的区域吗?因此,总而言之,我认为在 findAll() 函数中与 SELECT 子句一起使用时计算的属性很糟糕。 borked 我的意思是......坏了:)
  • 50 分给你,克里斯确认了这个错误,尽管它;轮子很棒,我感谢您为此所做的所有工作和努力。谢谢。
  • @MichaelGiovanniPumo,干得好。这是哪个版本的 CFWheels?
  • @HenriqueOrdine 它是 CFWheels 1.1.8(撰写本文时的最新版本)。
【解决方案2】:

我不熟悉你的框架,但试试这个:

<cfset property(name="linkUpVoteCount", sql="(SELECT COUNT(*) FROM votes WHERE votes.linkID = links.linkID AND voteType = 1) as linkUpVoteCount") />

as linkUpVoteCount 别名放在 sql 子查询之外。

另外,我很确定您不需要&lt;cfset&gt;'s 末尾的所有那些/&gt; 斜线。 html5 ftw。

【讨论】:

  • Wheels 获取 sql 参数中的内容并将 AS propertyName 添加到末尾。所以你最终会得到两个 AS。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-06
  • 2019-04-16
  • 2023-03-21
相关资源
最近更新 更多