⚠ 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

Call Back before insert Problem



groceryfan
  • profile picture
  • Member

Posted 12 February 2017 - 14:35 PM

My Controller, inserts a row in invHdrData Table,

i want the Max Doc Number in that Table to insert a Doc Number automatically,

The callback before insert not works. So Where is the problem? I would greatly appreciate it if you kindly give me some answers :)

 

My Controller :

public function sanads()
{
$crud = new grocery_CRUD();
$crud->set_theme('datatables');
$crud->set_table('invhdrdata');
$crud->set_subject('BLAH BLAH BLAH');
$crud->columns('FiscalYear','StoreNo','DocType','CreateDate','UpdateDate');
$crud->fields('FiscalYear','StoreNo','DocType','DocNo','CreateDate','UpdateDate');
$crud->callback_before_insert(array($this,'maxDocNoCon'));
$crud->field_type('DocNo','invisible');
$output = $crud->render();
$this->_example_output($output);
}

Another Function in Controller:

public function maxDocNoCon($post_array) {
$this->load->model('inv_model');
$post_array['DocNo'] = $this->inv_model->maxDocNoMod($post_array['FiscalYear'],$post_array['StoreNo'],$post_array['DocType']);
$test['DocNo'] = (int)explode(",", $post_array['DocNo'])+1;
return $test;
}

My Model:

function maxDocNoMod($FiscalYear,$StoreNo,$DocType){
$this->db->select_max('DocNo');
$this->db->where('FiscalYear', $FiscalYear);
$this->db->where('StoreNo', $StoreNo);
$this->db->where('DocType', $DocType);
$data = $this->db->get('invhdrdata');
if ($data->num_rows() != 1) {
// there should only be one row - anything else is an error
return false;
}
return $data->row()->DocNo;
}

Here is the StackOverFlow Link :

http://stackoverflow.com/questions/42180504/call-back-before-insert-not-work-in-grocery-crud

 


groceryfan
  • profile picture
  • Member

Posted 12 February 2017 - 17:19 PM

Problem solved,

I changed the controller to the following:

public function maxDocNoCon($post_array) {
$this->load->model('inv_model');
$post_array['DocNo'] = $this->inv_model->maxDocNoMod($post_array['FiscalYear'],$post_array['StoreNo'],$post_array['DocType'])+1;
if ($post_array['DocNo'] == 1){
$post_array['DocNo'] = 10001;
}
return $post_array;
}