Fast Uncompromising Discussions. FUDforum will get your users talking.
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
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.
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.