【问题标题】:Troublesome coldfusion union query in CFC with mySQL database使用 mySQL 数据库在 CFC 中遇到麻烦的冷融合联合查询
【发布时间】:2010-06-22 23:38:55
【问题描述】:

这适用于 mySQL 后端

在表格中...

   <cfselect name="to" size="1" bind="cfc:cfcs.messages.getOrganisations()" bindonload="yes" value="organisationID" display="organisationName" required="Yes">
     </cfselect>

在cfc中

<cffunction access="remote" name="getOrganisations" output="false" returntype="query" displayname="Get organisations list" hint="This method returns a list of organisations as a query.">
    <cfquery name="getOrganisations" datasource='myData'>

    SELECT organisationID, organisationName, acceptsReferral, metadataTemplate
    FROM organisations
    WHERE acceptsReferral
    ORDER BY organisationName ASC;

    </cfquery>
    <cfreturn getOrganisations>
</cffunction>

但如果我尝试

<cffunction access="remote" name="getOrganisations" output="false" returntype="query" displayname="Get organisations list" hint="This method returns a list of organisations as a query.">
    <cfquery name="getOrganisations" datasource='myData'>
    SELECT '0' AS organisationID, 'Select' AS organisationName, false AS acceptsReferral, 0 AS metadataTemplate
    FROM organisations
    UNION
    (SELECT organisationID, organisationName, acceptsReferral, metadataTemplate
    FROM organisations
    WHERE acceptsReferral
    ORDER BY organisationName ASC)

    </cfquery>
    <cfreturn getOrganisations>
</cffunction>

为了尝试选择查询的前导行,我在 firebug 中收到此 AJAX 错误“JSON 序列化失败:无法将二进制数据序列化为 JSON。”

Henry 的建议解决了上述问题,但我再次对下一点有点困惑,试图将两个选择链接起来。

这行得通...

<cfselect name="attentionOf" size="1" bind="cfc:cfcs.messages.getOrganisationMembers({to})" bindonload="false" value="userID" display="name" required="No" queryPosition="below">
<option value="0">Select</option>
</cfselect>

...但是如果我尝试使用绑定字段传入 DSN,我会从中得到“错误解析绑定”

<cfselect name="attentionOf" size="1" bind="cfc:cfcs.messages.getOrganisationMembers({to}, 'mySqlData')" bindonload="false" value="userID" display="name" required="No" queryPosition="below">
    <option value="0">Select</option>
</cfselect> 

【问题讨论】:

  • 如果您在本地调用 cfquery 是否有效? p.s.不要忘记 var-scope 你的“getOrganisations”变量。
  • 是的,如果我在本地调用,它会列出顶部带有 select 的查询,但是对于两个查询中的第二个,它无法 ORDER BY。所以 SQL 不会出错。我不明白 JSON 错误。

标签: mysql ajax coldfusion union cfc


【解决方案1】:

哦,我想这就是你想要的。

<cfset organisations = createObject("component", "cfcs.messages").getOrganisations()>

<cfselect name="to" query="organisations" 
          value="organisationID" display="organisationName"
          required="Yes" queryPosition="below">
    <option value="0">Select</option>
</cfselect>

如果你真的需要它来使用 bind, 试试queryPosition 属性是否有效。如果queryPosition 不适用于bind,那么您需要插入额外的虚拟行。将 unionQuery of Queries 一起使用可能更容易。

【讨论】:

  • 做到了,干得好亨利。我已经将链接数据绑定到 OP 中添加了我的持续困境
  • 为什么要通过DSN??没有意义。 DSN 通常应该在您的应用程序范围内。如果你真的需要传递一个变量,尝试在JS中设置DSN = 'mySqlData',然后在绑定表达式中只放DSN w/o quote 试试?
  • 嗯,我将 DSN 传递给 CFC 以避免任何依赖,因为您可以说我对 CFC 有点陌生,但认为最好不要有任何应用程序,还是 CFC 本身中的会话变量?我会尝试将其作为不带引号的变量
  • 使用远程代理。因此,不要只是将功能更改为 Remote,而是将其恢复为 Public。创建一个包含远程方法的新精简 CFC,并使用应用程序范围填充 DSN 等内容,并为您调用实际方法。 ColdSpring会在这种情况下帮助你,你可以看看。 coldspringframework.org/coldspring/examples/quickstart/…
猜你喜欢
  • 2011-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-25
  • 1970-01-01
  • 2022-12-23
  • 1970-01-01
相关资源
最近更新 更多