Insertorupdate and autoincrement [message #1792] |
Thu, 06 November 2008 05:46 ![Go to next message Go to next message](/theme/default/images/down.png) |
bonzo_bcn
Messages: 152 Registered: June 2008
|
Senior Member |
|
|
I think there is a bug in the insertOrUpdate function.
I have a field in the table where I'm inserting te record that is autoincrement and pk.
With this code:
if ($existing_participante_id > 0){
$insert_array['participante_id'] = $existing_participante_id;
}
$insert_array['tippart'] = 'E';
$insert_array['catsalut'] = $data['catsalut'];
$insert_array['nompart'] = $data['nompart'];
$insert_array['apellido1'] = $data['apellido1'];
$insert_array['apellido2'] = $data['apellido2'];
$insert_array = $participante->insertOrUpdate($insert_array);
I don't get returned the generated autoincrement in $insertarray
on the other hand, if I do it like this:
if ($existing_participante_id > 0){
$insert_array = $participante->updateRecord($insert_array);
}else{
$insert_array = $participante->insertRecord($insert_array);
}
the autoincrement is returned in $insert_array
|
|
|
Re: Insertorupdate and autoincrement [message #1793 is a reply to message #1792] |
Thu, 06 November 2008 09:09 ![Go to previous message Go to previous message](/theme/default/images/up.png) ![Go to next message Go to next message](/theme/default/images/down.png) |
AJM
Messages: 2361 Registered: April 2006 Location: Surrey, UK
|
Senior Member |
|
|
The behaviour you describe is as expected. Columns with the auto_increment attribute act as follows:
(a) During an INSERT if an auto_increment column has a value which is greater than zero then that value will be used, and a new one will not be generated. It is normal practice not to supply any value, not even zero, for an auto_increment column.
(b) The insertOrUpdate() function will construct a WHERE string using the current primary key and perform a lookup to find out if the record exists or not. If the primary key is auto_increment then for an INSERT there is no value, so the result of the lookup is unpredictable.
In short, do not use the insertOrUpdate() function to insert records which rely on auto_increment to supply their values. In this case you should examine the value yourself and call either insertRecord() or updateRecord() manually.
Tony Marston
http://www.tonymarston.net
http://www.radicore.org
|
|
|
Re: Insertorupdate and autoincrement [message #1803 is a reply to message #1793] |
Sat, 08 November 2008 11:11 ![Go to previous message Go to previous message](/theme/default/images/up.png) |
AJM
Messages: 2361 Registered: April 2006 Location: Surrey, UK
|
Senior Member |
|
|
I have modified the insertOrUpdate() method so that it calls isPkeyComplete() before performing the database lookup. If the primary key is not complete, such as when an auto_increment column has not been specified, then it will skip the lookup and force an INSERT.
It is therefore imperative that when wanting to perform an INSERT on a table which contains an auto_increment key that the column does not appear in the field array. If it appears with any value, even NULL or ZERO, then isPkeyComplete() will return TRUE and a lookup will be performed.
Please see the attached file for the patched code.
Tony Marston
http://www.tonymarston.net
http://www.radicore.org
|
|
|