Jump to content


callback_insert and wrong primary key after saving (found fix)

insert callback primary key

  • Please log in to reply
1 reply to this topic

#1 saulimus

saulimus

    Advanced Member

  • Members
  • PipPipPip
  • 40 posts

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. :)

#2 dblanco

dblanco

    Member

  • Members
  • PipPip
  • 22 posts

Posted 08 December 2012 - 09: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 http://www.grocerycr...allback-insert/





Also tagged with one or more of these keywords: insert, callback, primary, key

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users