0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ColdFusion コンポーネント使い方

Last updated at Posted at 2021-05-07

ColdFusion コンポーネント使い方

cfcにDBの作業や、if等の分岐作業をまとめておく。
その際のcfcomponentの使い方。

cfmで使う側

  • cfinvoke cfcのどのfunctionを使うか指示する
    • method cfcのfunction要素
    • component cfcのファイル名
    • returnVariable 戻り値を受け取る名前
  • cfinvokeargumentで渡したい引数を準備する(ないならなし)
    • form送信した内容をusernameに入れて引数で渡している。
<!---ユーザ追加のデータ渡す--->
    <cfif isDefined("form.action") and form.action is "add">
        <cfinvoke  method="createuser" component="login_function" returnvariable="message">
            <cfinvokeargument  name="username"  value="#form.username#">
            <cfinvokeargument  name="password"  value="#form.password#">  
        </cfinvoke>
        <cfoutput>#message#</cfoutput>     
    </cfif>

    <h2>ユーザの追加</h2>

    <form action="<cfoutput>#cgi.script_name#</cfoutput>" method="post">
        <input type="hidden" name="action" value="add">
        ユーザー名:<input type="text" name="username" size="20"><br>
        パスワード:<input type="password" name="password" size="20"><br>
        <br>
        <input type="submit" value="追加">
    </form>

cfcで作業する側

  • cfcomponent コンポーネントはまとめて1つでOK
    • 名前を付けても多分使わない。cfmで使う名前はファイル名 login_function.cfcを使うので。
  • cffunction
    • name cfmで付けた名前で
    • access remote/public/private…
    • returntype 戻り値の型。 複数⇒struct、DB参照の結果⇒query
  • cfargument 引数の受け取り(ないならなし)
    • name 名前を付ける。cfmと一緒の方が分かりやすい
    • type 型
    • required 引数の受け取りが必須かどうか
  • cfreturn 戻り値(ないならなし)
    • 返したいものを記入する。DBの結果ならcfqueryにつけた名前。
<!---ユーザの追加--->
<cfcomponent>
    <cffunction  name="createuser" access="remote" returntype="string">
        <cfargument  name="username" type="string" required="true">
        <cfargument  name="password" type="string" required="true">
    
    <cfquery datasource="sample" name="adduserck">
    select *
    from accounttable
    where username = '#arguments.username#'
    </cfquery>

    <cfif adduserck.recordcount gt 0>
        <cfset message = "#arguments.username#様は既に存在しているため登録できません">
        <cfelse>

        <cfquery datasource="sample" name="useradd">
        insert into accounttable values (
            '#arguments.username#', '#encrypt(arguments.password, "aaa")#',null,null,null,0,0,null,0,null
        )
        </cfquery>
        <cfset message = "#arguments.username#様を追加しました">
    </cfif>
    <cfreturn message>
</cffunction>
</cfcomponent>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?