Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Try Vanilla Forums Cloud product

Is it possible to know if a Gdn_Model::Save() was successful, without using an autoincrement key?

businessdadbusinessdad Stealth contributor MVP

I've struggling with an apparently failing Save() statement for a couple of hours, only to find out that it actually saves data correctly, but it doesn't return what one would expect. Environment is Vanilla

The table against which the data is saved does not have an autoincrement primary key, but a UUID field, which is passed whenever Gdn_Model::Save() is called (i.e. both for Insert and Update operations). When the data is saved correctly, the following code is executed (source class.database.php):

      // Did this query modify data in any way?
      if ($ReturnType == 'ID') {
         $this->_CurrentResultSet = $this->Connection()->lastInsertId();
      } else {
         if ($ReturnType == 'DataSet') {
            // Create a DataSet to manage the resultset
            $this->_CurrentResultSet = new Gdn_DataSet();
            $this->_CurrentResultSet->Connection = $this->Connection();

The issue is on the line that says $this->_CurrentResultSet = $this->Connection()->lastInsertId();. Since there is no autoincrement primary key, there will never be a "last insert ID". Therefore, the method will always return zero as a result, making it impossible to determine if the save was successful (unless one queries the database to check if the data is there, but that would mean doubling the amount of queries).

Adding an autoincrement key to the table would not be a viable workaround. The library I'm working must perform an "INSERT OR UPDATE" import, which means that some rows must be overwritten if they have changed. Since the autoincrement field would not exist in the source table, every Gdn_Model::Save() would trigger an INSERT, thus creating a lot of duplicates.

Back to the question, is there a (reliable) way to determine the result of a Gdn_Model::Save operation, without relying on autoincrement keys?



Sign In or Register to comment.