⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

callback_insert and wrong primary key after saving (found fix)



saulimus
  • profile picture
  • Member

Posted 10 August 2012 - 10:25 AM

Hi,

When you add a new entry and press save, the success message includes a link to modify the inserted entry or go back to list. However, if I use callback_insert, the link will always point to /edit/1. This is because of the following code in libraries/grocery_crud.php:


protected function db_insert($state_info)
.........
$callback_return = call_user_func($this->callback_insert, $post_data);

if($callback_return === false)
return false;
}
}

if(isset($insert_primary_key))
return $insert_primary_key;
else
return true;


Because primary key is not set, it simply returns true, which is equal to 1.
In order to fix this, I simply modified the code above like this:

if($callback_return === false)
return false;
/* Added this line: */
else $insert_primary_key = $callback_return;


So now I can return the inserted primary key in the callback and the link is correct. :)

dblanco
  • profile picture
  • Member

Posted 08 December 2012 - 21:34 PM

In this method, one can not simultaneously use callback_before_insert and callback_insert. When specifying a function callback_insert enters the code that you modified where the function callback_before_insert is not executed .

See /topic/1146-callback-before-insert-callback-insert/