Inserting Information into a Junction Table ColdFusion

心已入冬 提交于 2019-12-11 21:28:03

问题


On a Blog post page I add Title, Body, and UserID (Foreign Key from Authors Table) into my BlogPost Table:

<!--- Query to Insert Blog --->
<cfquery Result="blogEntry" datasource="prpblog">
    INSERT INTO BlogPosts (Title, BlogBody, UserID)
    VALUES
    (
        <cfqueryparam value='#Form.Title#' cfsqltype="cf_sql_varchar"/>,  
        <cfqueryparam value='#Form.BlogBody#' cfsqltype="cf_sql_varchar"/>, 
        <cfqueryparam value='#Form.SelectAuthor#' cfsqltype="cf_sql_numeric"/>
    )
</cfquery>

Then I want to add which tags the blog consists of into a Junction table (because of the many-to-many relationship caused by tags belonging to multiple blogs/blogs with multiple tags)

I know how to insert the tag into the junction table, but how would I insert my blogID into this table? The BlogID is an autonumber when a new blog is entered.

Junction Table column example:

    BlogID  TagID
    10          1
    10          10
    10          13
    9           10
    9           1

<Select name="SelectTag" multiple="multiple" size="6">
<cfoutput QUERY="Tags"><option value="#TagID#">#Tag#</option></cfoutput>
</select>

<cfquery name="AddTag" datasource="prpblog">
    INSERT INTO BlogTagJunction (TagID)
    VALUES
    (
                <cfqueryparam value='#Form.???' cfsqltype="cf_sql_numeric"/>
        <cfqueryparam value='#Form.SelectTag#' cfsqltype="cf_sql_numeric"/>
    )
</cfquery>

回答1:


I solved the problem myself by making a query statement after the insert statement for the blog that grabs the blog ID from the blog I just created:

That will only work if there is a unique index on the Title column. However, it is likely you do not even need a separate query. If BlogID is some sort of auto-incrementing column (identity, auto increment, ...), use the result attribute of cfquery to retrieve the generated ID value from your first INSERT statement. The exact syntax is db dependent.

Given that the source of TagID is another database table, it is simpler to skip looping and use a INSERT/SELECT to generate the junction table records in a single statement. You did not state your DBMS, but for SQL Server something like this. Change the "cfsqltypes" as needed:

  <!--- create the blog entry --->
  <cfquery result="blogEntry" ....>
       INSERT INTO BlogPost ( Title, Body, UserID )
       VALUES  
       (
         ... the values here  ....
       )
  </cfquery>

  <!--- associate the new blog entry with the selected tag id's --->
  <cfquery ....>
     INSERT INTO BlogTagJunction (BlogID, TagID)
     SELECT  <cfqueryparam value="#blogEntry.IDENTITYCOL#"  
                        cfsqltype="cf_sql_integer">
            , TagID
     FROM  YourTagTable
     WHERE TagID  IN
           (
              <cfqueryparam value="#Form.SelectTag#" 
                   list="true" 
                   cfsqltype="cf_sql_numeric"/>
           )
  </cfquery>

NB: Be sure to wrap both queries in a cftransaction to ensure data integrity.

Update from comments:

If you are using MS Access, then using the result attribute is out. CFQuery does not support that feature for MS Access. So you need to use an alternative method for grabbing the newly created ID.

As discussed in this link , there are several ways to do that. One of them is adding a UUID column to your table. Another option for MS Access is using the @@identity variable. After you insert a record into a table with an "Autonumber" column, the @@identity value will contain the ID of the new record. This blog contains an example. NB: As I noted in your other thread, you must enclose BOTH queries in cftransaction or it will not work correctly.

In summary you need to do three things:

  1. INSERT a new record into the BlogPost table
  2. Grab the new BlogID you just created
  3. Insert the selected tagID's into BlogTagJunction using the new id from step #2

Again, with MS Access you cannot skip the cftransaction here or it will not work. Putting it all together:

<cftransaction>

    <!--- create the blog record --->
    <cfquery result="blogEntry" ....>
        INSERT INTO BlogPost ( Title, Body, UserID )
        VALUES  
        (
        ... the values here  ....
        )
    </cfquery>

    <!--- get the ID of the record you just inserted --->
    <cfquery name="getNewRecord">
        SELECT  @@identity AS TheNewID
    </cfquery>

    <!--- Use that new ID to associate blog and selected tag id's --->
    <cfquery ....>
        INSERT INTO BlogTagJunction (BlogID, TagID)
        SELECT  <cfqueryparam value="#getNewRecord.TheNewID#"  
                    cfsqltype="cf_sql_integer">
               , TagID
        FROM  YourTagTable
        WHERE TagID  IN
        (
            <cfqueryparam value="#Form.SelectTag#" 
               list="true" 
               cfsqltype="cf_sql_numeric"/>
        )
    </cfquery>

</cftransaction>



回答2:


I solved the problem myself by making a query statement after the insert statement for the blog that grabs the blog ID from the blog I just created:

<CFQUERY name="BlogID" datasource="blog">
SELECT BlogID
FROM BlogPosts
WHERE Title = '#Form.Title#'
</CFQUERY>  

Then I do a CFoutput to output the queried BlogID Value.



来源:https://stackoverflow.com/questions/22465173/inserting-information-into-a-junction-table-coldfusion

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!