Radicore Forum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » RADICORE development » Framework » problem with updateRecord()
problem with updateRecord() [message #4386] Fri, 25 July 2014 12:21 Go to next message
htManager is currently offline  htManager
Messages: 433
Registered: May 2014
Senior Member
I have the following code in the _cm_post_insertRecord() function. I want to change values in a parent table after inserting an additional occurence in the child table.
But the values aren't updated with $dbobject->updateRecord ($data);

function _cm_post_insertRecord ($rowdata)
// perform custom processing before database record is inserted.
// if anything is placed in $this->errors the insert will be terminated.
{
// verfügbare (+1), belegte (+1) und freie Plätze (-1) werden aktualisiert
// get contents of foreign table SPIELE_TRANSPORT
$dbobject =& RDCsingleton::getInstance('spiele_transport'); // Instanz für Lookup-Tabelle
$where = "verbaende_art_id='{$rowdata['verbaende_art_id']}' AND
verbaende_kuerzel='{$rowdata['verbaende_kuerzel']}' AND
vereine_kuerzel='{$rowdata['vereine_kuerzel']}' AND
saison_id='{$rowdata['saison_id']}' AND
mannschaften_id='{$rowdata['mannschaften_id']}' AND
spiel_seq_no='{$rowdata['spiel_seq_no']}' AND
transport_no='{$rowdata['transport_no']}'";

$data = $dbobject->getData ($where);

if (!empty($data)){
$data = $data[0];
$data['transport_plaetze_belegt'] += 1;
$data['transport_plaetze_frei'] -= 1;
$dbobject->updateRecord ($data);

} // if

return $rowdata;

} // _cm_post_insertRecord

I know that in the std.tbl.class.inc the function updateRecord() comes with $fieldarray. But I don't know how to handle it.

What is wrong with my code?
Many Thanks in advance.
Re: problem with updateRecord() [message #4387 is a reply to message #4386] Sat, 26 July 2014 05:34 Go to previous messageGo to next message
AJM is currently offline  AJM
Messages: 2367
Registered: April 2006
Location: Surrey, UK
Senior Member
I cannot tell what is wrong with that code just by looking at it as its behaviour is affected by both the data being input and the contents of the database, neither of which I have. You need to step through with your debugger to see what is happening. For instance, does the call to $dbobject->getData($where) just before the call to $dbobject->updateRecord($data) actually retrieve anything?

Re: problem with updateRecord() [message #4388 is a reply to message #4387] Sat, 26 July 2014 06:02 Go to previous messageGo to next message
htManager is currently offline  htManager
Messages: 433
Registered: May 2014
Senior Member
Yes it retrieves everything. I can change the data and output the changed data with $this->errors.
But I can't output the data with var_dump($data);
Could it be a problem with a two dimensional array?
Re: problem with updateRecord() [message #4389 is a reply to message #4388] Sat, 26 July 2014 06:27 Go to previous messageGo to next message
htManager is currently offline  htManager
Messages: 433
Registered: May 2014
Senior Member
Or is there a better function to place the code?
I looked at your UML diagrams (UML diagrams for the Radicore Development Infrastructure) and thought that the _cm_post_insertRecord() funcition would be best.
The same (parent table) update process should also be possible with the _cmp_post_deleteRecord() method.
Re: problem with updateRecord() [message #4390 is a reply to message #4389] Sat, 26 July 2014 09:16 Go to previous messageGo to next message
AJM is currently offline  AJM
Messages: 2367
Registered: April 2006
Location: Surrey, UK
Senior Member
You need to step through with your debugger to see what happens when the code actually runs.

If you are trying to perform some extra processing after a record has been inserted then the _cm_post_insertRecord() is definitely the best place to put it. The _cm_post_deleteRecord() should only be used when you want to perform some extra processing after a delete.


Re: problem with updateRecord() [message #4417 is a reply to message #4390] Fri, 01 August 2014 11:59 Go to previous messageGo to next message
htManager is currently offline  htManager
Messages: 433
Registered: May 2014
Senior Member
The problem is solved. It was my own code which caused the problems. As I stepped through the Radicore code and I saw that everything works fine I discovered that the function _cm_validateUpdate() was called. I have code in this function to change some data when the record is accessed and changed via the normal UPDATE pattern.
Is it a good idea to check if the code should be executed with a $_SESSION variable? Or is there a better way?
Re: problem with updateRecord() [message #4418 is a reply to message #4417] Fri, 01 August 2014 12:06 Go to previous messageGo to next message
AJM is currently offline  AJM
Messages: 2367
Registered: April 2006
Location: Surrey, UK
Senior Member
What do you mean by "Is it a good idea to check if the code should be executed"? The code in the _cm_validateUpdate() method is only called when you are performing an update which needs to be validated before it is executed. That code is not called during a read, insert or delete, so it is valid during an update.

Re: problem with updateRecord() [message #4419 is a reply to message #4418] Fri, 01 August 2014 12:22 Go to previous messageGo to next message
htManager is currently offline  htManager
Messages: 433
Registered: May 2014
Senior Member
I know. But I have two different 'types' of updates; at least I think so.
The first one is when I update the record directly and I have to perform a few calculations for three fields.
The second one is when I call the update method after inserting or deleting a record in the child table which also causes the parent table to be updated.
But if I understand you right you mean that it would be best to change the code in that way that there is no matter from where the method _cm_validateUpdate() is called. If the method is called it will always updated in the right way.
I think I will do so, at least I will try it.
Thanks for your advice.
Re: problem with updateRecord() [message #4420 is a reply to message #4419] Fri, 01 August 2014 12:37 Go to previous messageGo to next message
AJM is currently offline  AJM
Messages: 2367
Registered: April 2006
Location: Surrey, UK
Senior Member
If you want different validation rules depending on how the record is being updated then you need to create a subclass of your table class to contain the different code. Then all you have to do is to ensure that the code which updates the table requiring the alternative set of rules uses the subclass instead of the main class. I do this type of thing many times when I want a method to execute different code depending on who is calling that method.

Re: problem with updateRecord() [message #4421 is a reply to message #4420] Fri, 01 August 2014 12:49 Go to previous messageGo to next message
htManager is currently offline  htManager
Messages: 433
Registered: May 2014
Senior Member
Thank you for the hint. But the code is in this case the same.
What happens if I call the updateRecord() method of the parent table in the _cm_post_insertRecord() method of the child table. Is the new inserted record almost 'visible' by the query?
Re: problem with updateRecord() [message #4422 is a reply to message #4421] Fri, 01 August 2014 12:55 Go to previous message
htManager is currently offline  htManager
Messages: 433
Registered: May 2014
Senior Member
The inserted record is visible. Everything works fine now.
Have a nice weekend.
Previous Topic: PROBLEM WITH DECIMAL POINT
Next Topic: pdf.styles.inc
Goto Forum:
  


Current Time: Thu Nov 21 05:03:02 EST 2024

Total time taken to generate the page: 0.01403 seconds