CodeCharge Studio
search Register Login  

Visual Web Reporting

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

YesSoftware Forums -> CodeCharge Studio -> PHP

 Need to INSERT into other table After Execute Update event for editable grid

Print topic Send  topic

Author Message
teufel

Posts: 13
Posted: 06/30/2004, 7:58 AM

Hi,

I have an editable grid with only UPDATE allowed.

After the user changes whatever values he wants in the grid, he then hits the UPDATE button, and besides the normal updating to the table that should occur (this table contains CURRENT values), I should also INSERT some new values into another table (this table should contain HISTORIC values). I don't have stored procedures at hand, so that's the reason why I need to do it programatically.

I tried using AFTER EXECUTE UPDATE event with no success.

I have to be sure the values are already updated in the database in order to insert into the other table, that's why I think I need to read all the updated values from the database after the update is completed, instead of reading them from the grid.

I could either insert only entries relating to the ones updated, or I could also insert all of them regardless of if the value was indeed or not updated... (though the first option would be best).

I tried composing a SELECT to read in all the current values from the updated table, and then perform an INSERT where ALL these rows would be inserted into the historic table, but I don't know where to put such a code, since After Execute Update seems to work after each row... Well, that would also work, I just have to know how to retrieve the updated values for that specific row...

Also any tips on how to debug this stuff? (output the SQLs to the screen, etc.)

TIA

_________________
--teufel
View profile  Send private message
F. Sibaja
Posted: 06/30/2004, 1:30 PM

Why don't you just overwrite the update method?, that's the most simple alternative (not the better if you are thinking about to change the programming language of your project some day).
I always recommend to use store procedures since they execute as one transaction, and you avoid any inconsistence risk with them.
Why you can't use stored procedures?
klwillis


Posts: 428
Posted: 06/30/2004, 1:43 PM

Hopefully this will get you going ...

Within the 'After Execute Update' event you can do something like this...

$db = new clsDBHistoric();
$SQL = "INSERT INTO HISTORY .... " <--- (whatever SQL you need)
$db->query($SQL);
$db->close();


Hope that helps.

_________________
Kevin Willis, VP/CIO
HealthCare Information Technology Specialist
http://www.nexushealthcare.com
"Fast - Convenient - Quality-Care"

Medical Software Consulting Services
Email : klwillis@nexushealthcare.com
Skype : klwillis2006
View profile  Send private message
teufel

Posts: 13
Posted: 06/30/2004, 2:49 PM

I am using MySQL, that's why I am not using Stored Procedures :(

klwillis: that's exactly what I am doing, exactly as I used to do in other ocasions, but this time I just can't seem to be able to get ANYTHING inserted. I can also see no output to the screen by putting ECHOS in between the custom code. maybe it has something to do with the button properties? Somehow for this editable grid only options submit and cancel appear as valid operations for the button (no Update/Delete/Insert,...). So maybe the AfterExecuteUpdate code is not being executed... I'm getting crazy here...

How should I check if the AfterExecuteUpdate code is being executed?


_________________
--teufel
View profile  Send private message
DonB
Posted: 06/30/2004, 6:12 PM

Bad news - the Ed Grid updates every row that appears on-screen, not just
those in which you changed data. So that won't really get you what you
want, just by getting the update event to fire.

The Ed grid only has a Submit and Cancel button. Maybe you used the
"regular" grid previously and that's why you recall the Insert button?

--
DonB

logging at http://www.gotodon.com/ccbth, and blogging at
http://ccbth.gotodon.net


"teufel" <teufel@forum.codecharge> wrote in message
news:540e3356a59786@news.codecharge.com...
> I am using MySQL, that's why I am not using Stored Procedures :(
>
> klwillis: that's exactly what I am doing, exactly as I used to do in other
> ocasions, but this time I just can't seem to be able to get ANYTHING
inserted.
> I can also see no output to the screen by putting ECHOS in between the
custom
> code. maybe it has something to do with the button properties?
Somehow
> for this editable grid only options submit and cancel appear as
valid
> operations for the button (no Update/Delete/Insert,...). So maybe the
> AfterExecuteUpdate code is not being executed... I'm getting crazy here...
>
> How should I check if the AfterExecuteUpdate code is being executed?
>
>
> _________________
> --teufel
> ---------------------------------------
> Sent from YesSoftware forum
> http://forums.codecharge.com/
>

peterr


Posts: 5971
Posted: 07/01/2004, 1:35 AM

'After Execute Update' should work every time when a record is updated, which basically will happen for all rows in your grid if you don't allow Inserts, as Don mentioned.
The echo should be OK for debugging as well. You can even find the word "AfterExecuteUpdate" in the generated code (Page.php) and place the echo command around it just to prove to yourself that that code is executed. Though make sure that later you remove that code.
I would also recommend that you delete the whole Page.php, so that it properly regenerated, in case something got corrupted there.
_________________
Peter R.
YesSoftware Forums Moderator
For product support please visit http://support.yessoftware.com
View profile  Send private message
teufel

Posts: 13
Posted: 07/01/2004, 11:58 AM

Following Peter's suggestion, I composed a new identical page from scratch and guess what??? It works now....

As DonB said, the AfterExecuteEvent will fire for each and every row in your ED grid, so that I would endup with many unnecessary inserts into the history table. What I did to solve that was to compare the value recently updated (by reading it again) with the value in the history table that has the most recent date. Only if they are different I perform the insertion. I know this is much worse than using Stored Procedures, but that's what I could figure out with MySQL...

Since I have at least transactions at hand for this version of MySQL, I believe I could start and end a transaction inside some of the other events in the page, am I right? If so, where would be the correct places to start and end the transaction? Any guidelines to do that correctly?

FYI, here are the relevant parts of the code I ended up with:

Quote :
global $ed_grid_form;

//Current Date/Time
$current_date = date('Y-m-d H:i:s');

//DB connections
$DBinsert_historic_values = new clsMyConn();
$DBselect_current_values = new clsMyConn();
$DBselect_historic_values = new clsMyConn();

//Select current value relative to the current ED GRID ROW
$sql = "SELECT code, cost FROM current_table WHERE code = " . $ed_grid_form->code_hidden->getValue();
$DBselect_current_values->query($sql);
$DBselect_current_values->next_record();

//Select historic value with NEWEST DATE relative to the current ED GRID ROW
$sql = "SELECT code, cost FROM historic_table WHERE code = " . $ed_grid_form->code_hidden->getValue() . " ORDER BY date DESC LIMIT 1";
$DBselect_historic_values->query($sql);
$DBselect_historic_values->next_record();

//Compares the CURRENT value with the HISTORIC value
//Only if values are different a new INSERTION at the history table will occur
if ($DBselect_current_values->f("cost") != $DBselect_historic_values->f("cost")) {
$sql = "INSERT INTO historic_table (code, date, cost) VALUES (". $ed_grid_form->code_hidden->getValue() . ", '" . $current_date . "', " . $ed_grid_form->cost->getValue() . ")";
$DBinsert_historic_values->query($sql);
}

//Free the connections
unset($DBinsert_historic_values);
unset($DBselect_current_values);
unset($DBselect_historic_values);


One last issue would be: the timestamp being recorded inside the history table is being setup inside the AfterExecuteUpdate event. Since the event fires every time for each row, I believe this timestamp could endup being set as different values for each row (although the rows are being timestamped with the exact same date/time, most probably because the execution is being really fast). Anyway, maybe I should declare a global timestamp when entering the page and then use that value, would that be more correct? How would I do that with PHP+CCS? (global $timestamp somewhere?).

Thank you.

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