开发者

CF9 EntityDelete: How to delete entities

开发者 https://www.devze.com 2022-12-19 00:16 出处:网络
If I have an array of entities, whats the easiest way of deleting the entire array of entities (or to put it this way, the entire ORM table)? I have:

If I have an array of entities, whats the easiest way of deleting the entire array of entities (or to put it this way, the entire ORM table)? I have:

<cfset allUsers = EntityLoad("User", {}, false)/>

Now to delete all the entities, would I use some sort of a loop? If so, how do I access individual entity primary keys within? 开发者_高级运维I tried:

<cfset userTemp = EntityLoad("User", allUsers[i].User.userID, true) /> 

but that didn't work...


EntityLoad will return an array of the entity objects so we can loop over that and use entityDelete:

<cfloop array="#allUsers#" index="User">
   <cfset entityDelete( User )>
</cfloop>

As ever when deleting data be careful! I generally prefer a soft delete.


If you wish to use array notation, you do it like this.

<cfloop from="1" to="#arraylen(allUsers)#" index="i">
    <cfset entityDelete( allUsers[i] )>
</cfloop>

Another way to do this without making ColdFusion do all the work is to execute a query.

<cfset ormexecutequery("DELETE FROM User",true)>


If deleting the array (in terms of freeing memory) is what you want to do, the following should work:

<cfset allUsers = "">
<!--- or --->
<cfset StructDelete(variables, "allUsers")>

Of course this would only remove one reference. If the objects (allUsers or the individual array members) are referenced someplace else, these references would continue to work.

0

精彩评论

暂无评论...
验证码 换一张
取 消