i'm trying to do a callback_before_insert, this is my script:

public function brandstof()
{
        $crud = new grocery_CRUD();

        $crud->set_theme('datatables');
        $crud->set_table('invoer');
        $crud->set_subject('Brandstof');
        $crud->required_fields('datum', 'aant_km', 'ltr', 'prijs');
                    $crud->fields('datum', 'aant_dagen', 'aant_km', 'ltr', 'prijs');
                    $crud->order_by('datum', 'desc');
                    $crud->callback_before_insert(array($this,'calculate'));

        $output = $crud->render();

        $this->_example_output($output);
}


    function calculate($post_array)
    {
        $post_array['tot'] = 100;
        $post_array['een_op'] = $post_array['aant_km'] / $post_array['ltr'];
        $post_array['ltr_per_100_km'] = $post_array['ltr'] / $post_array['aant_km'];
        return $post_array;
    }

the thing is that he only adds de regular fields, the one I type in myselfe. but the calulated ones are not inserted

for testing purpose I added this:

        $post_array['tot'] = 100;

just make the field 'tot' 100 after I insert this phpmyadmin givesnull values when it comes to the calculated fields

I can't see what I'm doing wrong

有帮助吗?

解决方案

This is a bit tricky.

So in grocery CRUD for security reasons you need to specify all the fields that you want to save at the fields method. Then you can simply have this type as invisible. As you can see at the documentation for invisible field there is an example for the callback_before_insert . So in your case you will simply have:

public function brandstof()
{
        $crud = new grocery_CRUD();

        $crud->set_theme('datatables');
        $crud->set_table('invoer');
        $crud->set_subject('Brandstof');
        $crud->required_fields('datum', 'aant_km', 'ltr', 'prijs');
        $crud->fields('datum', 'aant_dagen', 'aant_km', 'ltr', 
            'prijs','tot','een_op','ltr_per_100_km'); //Don't forget to add these fields

        $crud->field_type('tot','invisible');
        $crud->field_type('een_op','invisible');
        $crud->field_type('ltr_per_100_km','invisible');

        $crud->order_by('datum', 'desc');
        $crud->callback_before_insert(array($this,'calculate'));

        $output = $crud->render();

        $this->_example_output($output);
}


function calculate($post_array)
{
    $post_array['tot'] = 100;
    $post_array['een_op'] = $post_array['aant_km'] / $post_array['ltr'];
    $post_array['ltr_per_100_km'] = $post_array['ltr'] / $post_array['aant_km'];
    return $post_array;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top