CodeCharge Studio
search Register Login  

Visual PHP Web Development

Visually Create Internationalized Web Applications, Web Reports, Calendars, and more.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> General/Other

 Best way to prevent updates for unchanging data.

Print topic Send  topic

Author Message
cgosbee

Posts: 23
Posted: 10/16/2007, 6:00 PM

Does anyone have any pointers on a good way to prevent an update from occurring if the user didn't make any changes, but submits - resulting in a database update - anyway?
View profile  Send private message
mamboBROWN


Posts: 1713
Posted: 10/16/2007, 6:10 PM

cgosbee
One way would be to compare the fields to see if any of them were actually changed if not then give an error message.

Another way would be to add required fields that force the user to put in a value.

I am quit sure there are other ways but these are the first two that pop into my head. ;-)
View profile  Send private message
cgosbee

Posts: 23
Posted: 10/16/2007, 7:00 PM

Thanks mamboBROWN... I suspect you're ahead of me in your knowledge of CodeCharge.

I know I need to compare (Marking fields required doesn't work in this case, unfortunately) the old data and the new (unless there is something that actually tracks whether or not a user has changed anything in a given row, which would be absolutely incredible, and perfect), stopping the update if no changes having occurred. What I was really looking for some pointers on is the "right" place or method(s) to do this.



View profile  Send private message
JimmyCrackedCorn

Posts: 583
Posted: 10/16/2007, 10:18 PM

not sure which language you're using but in ASP you could do this as follows,

1) for each field you wish to monitor in your record form, add a hidden field
2) in the BeforeShow event of your record form assign the value of each field to its corresponding hidden field
3) in the BeforeUpdate event of your record form compare the value of each field to its corresponding hidden field and if none have changed then set UpdateAllowed=False


As an example let's say I have a form called Customer that only has one field, cName (which is associated with a database field). Add a hidden field called cNameHidden (which is not associated with a database field).

In Customer's BeforeShow event add the following custom code,

Customer.cNameHidden.Value = Customer.cName.Value  

That saves the original value of cName in the hidden field.

In Customer's BeforeUpdate event add the following custom code,

if CCGetParam("cName",Empty) = CCGetParam("cNameHidden",Empty) then  
    Customer.UpdateAllowed = False  
end if  

one of the local experts like Walter may be able to offer a better solution but I just tested and verified this will work. good luck.
_________________
Walter Kempees...you are dearly missed.
View profile  Send private message
wkempees


Posts: 1679
Posted: 10/17/2007, 7:45 AM

might be nice to do update.allowed false on Initialize
and for each field onchange, set to true?

just a sleepless hint


_________________
Origin: NL, T:GMT+1 (Forumtime +9)
CCS3/4.01.006 PhP, MySQL .Net/InMotion(Vista/XP, XAMPP)

if you liked this info PAYPAL me: http://donate.consultair.eu
View profile  Send private message
cgosbee

Posts: 23
Posted: 10/17/2007, 11:30 AM

I think JimmyCrackedCorn's suggestion is almost perfect (I had thought of what you suggested wkempees, but it does mean if someone so much as clicks an option in a drop-down, it will set the flag. If they then put it back, it will be set back, and the update I don't want will happen anyway.)

I have tried to implement the change, but can't quite get it working. I am using an editable grid. It appears the solution suggested only properly works with the first record in the grid. (probably because that is exactly what the code does... I just don't quite understand why) For the other records, it seems that the updateallowed flag is always false now, so it doesn't allow any updates on those records anymore.

I have done the following in PHP, hopefully this is equivalent to Jimmy's suggestion:

BeforeShow:

Customer->cNameHidden->Value = Customer->cName->Value;

BeforeUpdate:

if CCGetParam("cName","") == CCGetParam("cNameHidden","")
{
Customer->UpdateAllowed = False;
}

Is the editable grid my problem, or is the code above incorrect (or both?)? Any suggestions/help would be appreciated! Thanks again everyone!
View profile  Send private message
cgosbee

Posts: 23
Posted: 10/17/2007, 11:33 AM

Actually, it's even worse. It always updates the first record(even when it hasn't changed), and never updates the others.
View profile  Send private message
JimmyCrackedCorn

Posts: 583
Posted: 10/17/2007, 3:47 PM

using an editable grid probably complicates this significantly. not sure but you'd probably need to keep track of all fields of all rows and then decide whether or not to allow update based on that.

what is the exact condition under which you do NOT want the grid to do an update? if all fields on all rows remain unchanged? or, are you trying to do this row by row so that only the rows that have changed data will be updated while the ones that have not changed will not be updated? if the latter it is probably more complicated than I can help you with.
_________________
Walter Kempees...you are dearly missed.
View profile  Send private message
cgosbee

Posts: 23
Posted: 10/17/2007, 7:16 PM

Yep, it's the latter alright.

I didn't realize that setting that flag is basically a "shutdown" on the entire set of updates. I have some other sections where the system needs to do this for a single record... hopefully I can use your suggestion properly for that.

On the grid editing.... I guess I've got my work cut out for me....

Thanks for trying.
View profile  Send private message
JimmyCrackedCorn

Posts: 583
Posted: 10/17/2007, 9:51 PM

just curious. why do you care if a record gets updated to the same values as before?

since you are already handling multiple records at once using the editable grid it really should not matter. and, if you really need to know when something was last updated, how about setting a date/timestamp for each record and then check row by row before update for rows that did not get changed and then just reset their date/time stamp back to its original value and let it get "updated" with identical info?
_________________
Walter Kempees...you are dearly missed.
View profile  Send private message
estmatic

Posts: 6
Posted: 10/18/2007, 7:20 AM

Two thoughts:

1) I think you would need to use the beforeShowRow event instead of beforeShow when "caching" the initial values. The beforeShow only gets called once so you're probably only catching one record.

2) You are setting the updateAllowed flag to false after checking that the values haven't changed. Are you setting it back to true when something has changed?

(Java programmer here so I'm just guessing at the syntax for the else statement)
if CCGetParam("cName","") == CCGetParam("cNameHidden","")  
{  
   Customer->UpdateAllowed = False;  
}  
else  
{  
   Customer->UpdateAllowed = True;  
}


I looked through the EditableGrid and EditableGridProcessor code (again, Java programmer, I'm not sure what the equivalent files would be for PHP) to see how it handled updates. As far as I could tell it only checks the updateAllowed flag once, not on a per-row basis. Though that seems contradictory to what you found, "It always updates the first record(even when it hasn't changed), and never updates the others.", so I'm not sure.

Play around with #2 to see if that helps at all. If not then I think you'll need to edit some of the CodeCharge generated "common files" to do what you want.
View profile  Send private message
cgosbee

Posts: 23
Posted: 10/18/2007, 9:48 AM

Thanks everyone, a combination of Jimmy's suggestion and knowing the right place to look in the help (thank you Yes support!) did the trick.

We are tracking history of EVERYTHING that occurs with triggers. Needless to say, having said triggers fire for X records evertime someone changes a single record is not desirable.

Fortunately, there is only 1 field of importance on this grid, so a hidden field, set to the value of the field of import in Before Show gets me what I need to compare against.

The UpdateAllowed flag is only once per execution as noted by estmatic, so that doesn't work.

This help topic was just the trick:
Examples and Techniques -> Programming -> Customizing the Data Source -> Prevent Operation for Individual Records via Editable Grid

I set up a BeforeExecuteUpdate on the grid, and compared the value to insert with the hidden field. When they're the same, I just set CmdExecution to false, and that aborts the update. If only I'd known two days ago it was this easy!!

Example of Before Execute:
if ($Customer->cNameHidden->GetValue() == $Customer->cName->GetValue())  
{  
     $Customer->DataSource->CmdExecution = false;  
};

Again, thanks everyone for your help!
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

PHP Reports

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.