Looking for a PHP CRUD compatible with any Framework? Meet Grocery CRUD Enterprise 😍

Tutorial - Using Callbacks

grocery CRUD simply uses the call_user_func of PHP. So when you have for example:


this simply means that you insert as a callback the function "my_callback" from your controller. So it is the same thing like this:


So you can use whatever callback you like, for example:


This equals with :


or if you have PHP version greater or equal to 5.3 you can also use anonymous functions like for example:

$post_array['user_id'] = $this->session->userdata('user_id');
return $post_array;
or you can simply use a static method inside a class for example:
$crud->callback_column('buyPrice', 'Examples::valueToEuro');
which equals with:

The most common way to use it is like this:

This will simply call the :
Note: It is suggested to use the underscore (_) before the callback name. This is because if you add the underscore at a function in your Controller it is not accessible from the URL. The private and protected method are not accessible from the $this so be careful to not use callbacks as protected or private methods.

So how it works anyway?

All the callbacks are well documented with an example of usage at the API and functions section of the website. Each callback takes different parameter so you have to check the example first from the API.

Let's see an example of callback_before_update method.

function encrypt_password_callback($post_array, $primary_key) {
//Encrypt password only if is not empty. Else don't change the password to an empty field
$key = 'super-secret-key';
$post_array['password'] = $this->encrypt->encode($post_array['password'], $key);
return $post_array;
This simply means that before the update we will call the
$this->encrypt_password_callback($post_array, $primary_key);
where $this is the Controllers $this and $post_array, $primary_key are automatically inserted from the grocery CRUD library