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
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
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.