⚠ 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

callback_after_update - do the function only if a particular field is updated



Mathew Cherian
  • profile picture
  • Member

Posted 09 July 2015 - 09:56 AM

Hi there,

Just got a confusion here with callback_after_update. 

 

Please find the functions below.

 

 
public function invoices()
{
clearSearchCookies('invoices');
$crud = new grocery_CRUD();
$crud->set_theme('datatables');
$crud->set_table('invoice');
$crud->set_subject('Invoices');
$crud->edit_fields('hostingid','domainid','paid');
$crud->set_relation('customer','customers','company');
$crud->field_type('paid','true_false', array('No', 'Yes')); 
if($crud->getState() == 'edit') {
$crud->change_field_type('domainid', 'hidden');
$crud->change_field_type('hostingid', 'hidden');
$crud->callback_after_update(array($this, 'extendExpiry'));
$crud->unset_add();
$crud->unset_delete();
$crud->unset_export();
$crud->unset_print();
$output = $crud->render();
$this->_example_output($output); 
$this->output->set_template('admin_default');
}
 
 
function extendExpiry($post_array,$primary_key)
   {
$paid = $this->input->post('paid');   
$hostingid=   $this->input->post('hostingid'); 
$domainid=   $this->input->post('domainid');  
       if($paid='yes' && $hostingid) 
       { 
    $this->db
    ->set('expiry', 'DATE_ADD(expiry,INTERVAL 1 year)', FALSE)
    ->where('id', $hostingid)
    ->update('hosting');
       }
 
else if($paid='yes' && $domainid) 
    { 
  $this->db
    ->set('expiry', 'DATE_ADD(expiry,INTERVAL 1 year)', FALSE)
    ->where('id', $domainid)
    ->update('domain');
  }
       return true;
   } 
 

 

The problem is that whenever i update the  record, the function extendExpiry runs. I just want this function to run only if my 'paid' field is updated. It's now 'update changes' runs even if there is no update. I dont want that. 

 

Here in the above function I have only one field. But I would like to extend it when there are many fields.

 

If there is any workaround also I would be more than happy to implement. Just a newbie in GC.

 

Thanks,

Mathew. 

 


Paul Savostin
  • profile picture
  • Member

Posted 09 July 2015 - 20:01 PM

Hi! I am not clearly understand what you need but that is function run everytime when you save record - this is normal!

You need just find out do something you need has been change. For example, you can get old record by simple database query and compare old value and new one in post array.