confused about isDirty()

2 views
Skip to first unread message

Gareth Cole

unread,
Aug 25, 2009, 2:32:32 PM8/25/09
to reacto...@googlegroups.com

Hi,

 

I'm not sure if this is a bug, or I just don't understand isDirty() correctly. Basically, I want to determine if a record was found in the database, when I try to load it.

 

The following example code loads 3 User records. The only userID that exists in the database is 123.

 

<cfscript>

u1 = r.createRecord("User").load(userID=0);

u2 = r.createRecord("User").load(userID=123);

u3 = r.createRecord("User").load(userID=1234);

</cfscript>

 

However, when I call isDirty() on each record, only the record with userID=1234 returns true. The record with userID=0 should also be dirty, as no record exists for it, but this isn't the case.

 

I've done a little digging into the code, but can't figure out why this is happening yet. I'm keen to establish if this is expected behaviour, or a bug first.

 

Thanks,

 

Gareth

Chris Blackwell

unread,
Aug 25, 2009, 3:25:44 PM8/25/09
to reacto...@googlegroups.com
Hi Gareth,

Yes, looks like you are a little confused, i will try and explain.
isDirty tells you whether a record has been modified since it was loaded, not whether a record exists. 

lets look at a bit of your example code

u2 = r.createRecord("User").load(userID=123);

this is the same as writing

u2 = r.createRecord("User");    // isDirty is false for a new record
u2.setUserID(123);                    // isDirty is now true
u2.load();                                    // a successful load will reset isDirty to false

If you apply the same logic to the other examples you'll see whats going on

The method your after is record.exists()

u2 = r.createRecord("User").load(userID=123);

doesItExist = u2.exists();          // should be true

Its been a while since i fiddled with this stuff, and it may be possible to see if a record exists without calling load on it first, that'll save you a query.

u2 = r.createRecord("User").setUserID(123);
doesItExist = u2.exists();          // should be true, as long as userID is the only primary key

Hope that helps

Cheers, Chris

2009/8/25 Gareth Cole <garet...@esus.ie>

Gareth Cole

unread,
Aug 25, 2009, 3:42:30 PM8/25/09
to reacto...@googlegroups.com

Thanks Chris – I think I understand now.

 

So in the case of userID=0, the record was never made dirty, because 0 is the default value of userID anyway. That makes sense.

 

You're right. exists() is the method I should be using.

Reply all
Reply to author
Forward
0 new messages