⚠ 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

What is wrong with my callback_before_update?



clustersblue

clustersblue
  • profile picture
  • Member

Posted 09 October 2012 - 09:50 AM

[b]Scenario:[/b] I want to get all the values of the edit form before the user changes them. Then, after updating the form I'd like to compare each field (before and after) for changes. If there is a change to the field I'd like to record it in the database and I'm going to use them in the view page wherein the user will be able to see what they have changed on the particular record (some sort of a history).

[b]The Plan:[/b] Base on the documentation, I'm going to use the callback_before_update() function. The function runs before the auto update of the crud. Because of too many long line of codes I have then truncated (...)


$crud= new grocery_CRUD();

$crud->set_table('charter')
->set_subject('Charter')
->set_relation('operator_id','operators','short_name',array('disabled_id'=>'0'))
...

->order_by('charter.created_date','desc');

$crud->display_as('charter_id','Charter ID')
->display_as('charter_title','Test Charter')
->display_as('created_date','Date of Request')
->display_as('operator_id','Requester')
...

->display_as('configuration_requirements','Configuration Requirements');

$crud->columns('charter_code','charter_title',...');
$crud->add_fields('charter_title','testing_type_id',...');
$crud->edit_fields('charter_title','testing_type_id',...');
$crud->required_fields('charter_title','testing_type_id',...');

$crud->callback_before_update(array($this, '_perform_module_before_update'));
$crud->callback_after_insert(array($this, '_perform_module_after_insert'));
$crud->callback_after_update(array($this, '_perform_module_after_update'));
$crud->callback_edit_field('operator_id',array($this,'_operator_id_edit_field'));
$crud->callback_edit_field('comments',array($this,'_comments_edit_field'));

$output = $crud->render();


To test if the callback_before_update fires I here the function. And I am expecting to have a cookie with value from one of the fields.

[indent=1]

function _perform_module_before_update($post_array)
{
set_cookie(array('name' => 'charter_title','value' => $post_array['charter_title']));

}
[/indent]

[b]The Issue:[/b] As I am expecting a cookie to be created but there is none. It seems the callback_before_update didn't fired. Is there something I missed? or Is it wrong approach for this scenario? I'm open for any advice on how I'm going to work on this scenario.

Thanks and BR,
Clustersblue

victor

victor
  • profile picture
  • Member

Posted 09 October 2012 - 17:30 PM


$crud->callback_before_update(array($this, '_perform_module_before_update'));

function _perform_module_before_update($post_array, $primary_key)
{
$old_data = $this->db->get_where('your_table', array('id'=>$primary_key))->row_array();
if($old_data['your_field']!=$post_array['your_field'])
{
// your code
}

}