try/catch entityDelete()?

28 views
Skip to first unread message

rheldt

unread,
Oct 10, 2011, 10:28:36 AM10/10/11
to cf-orm-dev
Greetings everyone!

I'm having an issue with entityDelete() I'm trying to hammer out and
hopefully someone can point me in the right direction. Let's say I
have a CFC for handling songs, of which a song can be associated with
a category:

Song.cfc:
<cfproperty name="SongID" type="numeric" ormtype="integer"
notnull="true" fieldtype="id" generator="identity" />
<cfproperty name="SongCategory" cfc="SongCategory" fieldtype="many-to-
one" fkcolumn="SongCategoryID" />
<cfproperty name="Name" type="string" ormtype="string" length="100" />
<cfproperty name="Author" type="string" ormtype="string" length="100" /
>

SongCategory.cfc
<cfproperty name="SongCategoryID" type="numeric" ormtype="integer"
notnull="true" fieldtype="id" generator="identity" />
<cfproperty name="Name" type="string" ormtype="string" length="100" />

When someone deletes a song category, I'd like to be able to prevent
the user from doing so if its "in use" (associated with a song
record). Traditionally, I've done this with a try/catch around a SQL
DELETE statement. Since I'm using ORM, I figured I could put the try/
catch around entityDelete() to accomplish the same thing. For example:

transaction action="begin" {
.
.
local.objSongCategory =
entityLoad("SongCategory",SongCategoryID,true);
.
.
try {
entityDelete(local.objSongCategory);
} catch (Any e) {
transaction action="rollback";
}
}

However, when I attempt to delete a song category that's in use, I get
the following error:

Cannot delete or update a parent row: a foreign key constraint fails
(`mydsn`.`songs`, CONSTRAINT `songs_ibfk_3` FOREIGN KEY
(`SongCategoryID`) REFERENCES `songcategories` (`SongCategoryID`))

Yes, technically that error is correct... if the record was to be
deleted, there would be a constraint violation. However, error is
being thrown despite being inside a try/catch. Does anyone know why?
Am I doing something wrong?

Shannon Hicks

unread,
Oct 10, 2011, 10:33:53 AM10/10/11
to cf-orm-dev
You should probably use orm events. If you implement preDelete() and
have it return false, it does not delete the entity.

Shan

Andrew Scott

unread,
Oct 10, 2011, 10:34:39 AM10/10/11
to cf-or...@googlegroups.com
Do you have in your application.cfc flushAtRequestEnd set to false?

Have you tried setting that and trying again?


-- 
Regards,
Andrew Scott
WebSite: http://www.andyscott.id.au/




--
You received this message because you are subscribed to the Google Groups "cf-orm-dev" group.
To post to this group, send email to cf-or...@googlegroups.com.
To unsubscribe from this group, send email to cf-orm-dev+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cf-orm-dev?hl=en.



Mark Mandel

unread,
Oct 10, 2011, 7:00:45 PM10/10/11
to cf-or...@googlegroups.com
Unfortunately, yes, lots of things.
  1. Don't use exceptions in place of if/else blocks. It's really not what they are designed for, and as a general rule, a bad coding practice.
  2. If I assume that your ORM setup is the default one for CF, then your database changes will be batched at the end of the request. (Start of explanation here: http://www.compoundtheory.com/?action=displayPost&ID=415 )
Hope that helps.

Mark

On Tue, Oct 11, 2011 at 1:28 AM, rheldt <ryan....@gmail.com> wrote:
Am I doing something wrong?



--
E: mark....@gmail.com
T: http://www.twitter.com/neurotic
W: www.compoundtheory.com

cf.Objective(ANZ) + Flex - Nov 17, 18 - Melbourne Australia
http://www.cfobjective.com.au

2 Devs from Down Under Podcast

Reply all
Reply to author
Forward
0 new messages