The logic first : In order to achieve this you have to add a callback_before_insert() function in your grocery crud controllers' method. Inside this callback function you will create the random number you want, then add it to the $post_array variable and then return the $post_array back to your controllers' method (There are already examples on this at grocery cruds' official page).
So, somewhere in your controller you add this :
function _create_unique_secret_number()
{
/*Create a random secret_Number between 0 and 1000000
* and assign it to a variable
*/
$random_unique_secret_number = mt_rand( 0, 1000000 );
/* Now make sure that your random secret number is not already "in use"
* which means that the generated id is already stored in your table.
*/
$query = $this->db->where( 'secretNumber', $random_unique_secret_number )
->get_where( 'your_table_name_goes_here' );
if( $query->num_rows() > 0 )
{
$query->free_result();
// Try again in case the randomly generated number above is in use
return $this->create_unique_secret_number();
}
$post_array['secretNumber'] = $random_unique_int;
return $post_array;
}
/* And finally call the function inside your controllers' method.
* I mean inside the method that is handling your table.
*/
$crud->callback_before_insert( array ($this, '_create_unique_secret_number') );
You can then access the generated number in your grocery crud controller by accessing theenter code here
$post_array['secretNumber'] value ..