⚠ 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

Why GC not using inserted primary key in custom callback_insert?



Paul Savostin
  • profile picture
  • Member

Posted 28 August 2015 - 12:41 PM

Hi everyone! I face with certain situation - when using custom callback_insert function  then after u just click Save (Not insert and back to list!)

GC shows two links - edit record and back to the list like popup messages, and link "edit record" not working!

 

Explanation:

 

In GC when u use custom callback_insert and return inserted id by yourself (or true, no matter, everything except false, what means error while inserting)

present some logic if u use custom insert
 

//in my version GC 1.4.2

//if not use custom callback insert
if($this->callback_insert === null)
{
   ..... some login and after inserting creates inserted ID like

   $insert_result =  $this->basic_model->db_insert($insert_data);

   if($insert_result !== false)
   {
	$insert_primary_key = $insert_result;  
    }
    else
    {
        return false;
    }
}
else //here custom callback insert
{
    $callback_return = call_user_func($this->callback_insert, $post_data);

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

}

//finally check if inserted primary key exist
if(isset($insert_primary_key))
  return $insert_primary_key;
     else
  return true;

How we see above $inserted_id only gets in usual case and in custom callback return TRUE!

So this true casting to 1 and adds to edit link so when u click it there is always edit record with ID = 1 what terrible wrong! Why just not check what user callback return?

Something like this after calling custom function for insert:

$callback_return = call_user_func($this->callback_insert, $post_data);

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

//here we sure that this is not true so what this is? sure it can be only ID
elseif($callback_return !== true)
{
       $insert_primary_key =  $callback_return;
}

Maybe this all already resolve in some ways before i write this. Anyway thanks for atention:)

 


Amit Shah
  • profile picture
  • Member

Posted 29 August 2015 - 17:39 PM

Well my friend.. its time you too started discovering .. i know for a long time u always did.. but no code is perfect code.. it always have room for improvement and it always have a possibility of it being re-written in other way around.. u find in better ways.. lovely.. and u share it with others .. awsome .. ppl benifit from it.. bliss..

 

So as i always say .. HAPPY GCING :) ... to u my brother!!


Paul Savostin
  • profile picture
  • Member

Posted 29 August 2015 - 21:32 PM

Hi!!!!!!!!! :D  So u did know about that and dont tell anybody? ;)

I think this is just could be in legacy GC, why not? :D  Thanks a lot for discovering guide!