Callback before/after insert/update help plz
- Single Page
Posted 07 June 2012 - 19:01 PM
Here is my controller:
[php]
$this->load->view('header_view', $data);
$this->load->view('top_menu', $data);
$crud = new grocery_CRUD();
$crud->set_table('location_main');
$crud->columns('location_main_id','code','name','description','url','date_added','date_modified','status','image');
$crud->set_subject('Account');
$crud->fields('code','name','description','url','status','image');
$crud->add_action('View Account', 'http://localhost/enterprise/assets/images/view_action.gif', 'acctnloc/account_profile');
$crud->display_as('location_main_id','Main Location ID');
$crud->display_as('url','URL');
$crud->display_as('date_added','Date Added');
$crud->display_as('date_modified','Date Modified');
$crud->callback_before_insert(array($this,'get_init_date'));
$output = $crud->render();
$this->_example_output((object)array('output' => '' , 'js_files' => array() , 'css_files' => array()));
$this->load->view('account_list',$output);
$this->load->view('footer_view', $data);
[/php]
The database auto increments the location main id so I don't need to have it in the add or edit fields. But I want to set the date added and date modified fields to the current date when it gets added into the database. I have already set the database to auto change the date modified when ever it is updated.
I have tried both of these, but they don't put anything in the database:
[php]
function get_init_date($post_array){
$this->db->set('date_added',date('DD-MM-YYYY')); //type DATE in database
$this->db->set('date_modified',date('DD-MM-YYYY HH:mm:SS')); //type TIMESTAMP in database
$this->db->insert('location_main');
}
[/php]
[php]
function get_init_date($post_array){
$post_array['date_added'] = $this->db->set('date_added','CURDATE()'); //type DATE in database
$post_array['date_modified'] = $this->db->set('date_modified','NOW()'); //type TIMESTAMP in database
$this->db->insert('location_main');
return $post_array;
}
[/php]
Can anyone help me out with this?
Posted 07 June 2012 - 21:26 PM
$crud->fields('code','name','description','url','status','image','date_added','date_modified');
so now grocery CRUD can handle those two fields. But of course you don't want them to your form so you will have:
$crud->change_field_type('date_added','invisible');
$crud->change_field_type('date_modified','invisible');
So now this function:
function get_init_date($post_array){
$post_array['date_added'] = date('d-m-Y');
$post_array['date_modified'] = date('d-m-Y H:i:s');
return $post_array;
}
will work as expected.
Posted 08 June 2012 - 15:40 PM
I also found that doing an update after the insert works as well
[php]
function get_init_main_date($post_array,$primary_key)
{
$sqlStr = "UPDATE location_main SET date_added=CURDATE(),date_modified=NOW() WHERE location_main_id=" . $primary_key;
$query = mysql_query($sqlStr);
}
[/php]
Posted 08 June 2012 - 16:06 PM
In the controller I posted before I have an add_action function that calls the next controller and passes the $account_id like so:
[php]
function account_profile($account_id)
{
$data['user_id'] = $this->tank_auth->get_user_id();
$this->load->model('acctnloc_nec');
$this->acctnloc_nec->store_lmid($account_id,$data['user_id']);
$crud = new grocery_CRUD();
$crud->set_table('location');
$crud->set_model('location_crud_model');
$crud->add_action('View Location', 'http://localhost/enterprise/assets/images/view_action.gif', 'acctnloc/location_display');
$crud->columns('name','city','state','lease','meter_rental','maintenance','fee','total','postage','spend_ratio');
$crud->fields('location_code','number','parent_number','type','region','name','address1','city','state','zip_code','country_id','telephone','fax','status');
$crud->display_as('location_code','Location Code');
$crud->display_as('meter_rental','Meter Rental');
$crud->display_as('total','Total Expense');
$crud->display_as('spend_ratio','Spend Ratio');
$crud->display_as('maintenance','Maint.');
$crud->callback_after_insert(array($this,'get_init_loc_date'));
$crud->where('location_main_id', $account_id);
$output = $crud->render();
$this->_example_output((object)array('output' => '' , 'js_files' => array() , 'css_files' => array()));
$this->load->view('account', $output);
}
[/php]
My crud table does the same thing
So before the views get loaded I save the $account_id in the user table like so:
[php]
function store_lmid($account_id,$user_id)
{
$sqlStr = "UPDATE users SET cur_lmid=" . $account_id . " WHERE id=" . $user_id;
$query = $this->db->query($sqlStr);
}
function get_lmid($user_id)
{
$sqlStr = "SELECT cur_lmid FROM users WHERE id=" . $user_id;
$query = $this->db->query($sqlStr);
$lmid = $query->row_array();
return $lmid['cur_lmid'];
}
[/php]
I've checked the database and it does store it where I want it, but then I try to call another function to retrieve the data and send it my callback function and it retrieves a NULL variable.
[php] function get_init_loc_date($post_array,$primary_key)
{
$user_id = $this->tank_auth->get_user_id();
$this->load->model('acctnloc_nec');
$account_id = $this->acctnloc_nec->get_lmid($user_id);
$this->acctnloc_nec->insert_acct_loc($primary_key,$account_id);
}
[/php]
[php]
function insert_acct_loc($location_id,$account_id)
{
$sqlstr = "SELECT * FROM location_main WHERE location_main_id=" . $account_id;
$query = $this->db->query($sqlstr);
$acct = $query->row_array();
$sql = "UPDATE location SET location_main_id=" . $account_id . ",location_main_code='" . $acct['code'] . "',date_added=CURDATE(),date_modified=NOW() WHERE location_id=" . $location_id;
$query2 = $this->db->query($sql);
}
[/php]
Does it have to do with the session information? If so, how would I go about retrieving the data?
Posted 27 June 2013 - 07:34 AM
I have a problem with callback before insert. The post_array variable is ok and I can set up my date before insert. The problem is that CodeIgniter call twice this function. At the first time, the record inserted correctly into DB and at the second time, I reach an error: duplicate entry at..... because record has been insert at the first time. How can I prevent the second call? Or better: why this second call happen?
Kind regards,
Posted 27 June 2013 - 08:14 AM
I have a problem with callback before insert. The post_array variable is ok and I can set up my date before insert. The problem is that CodeIgniter call twice this function. At the first time, the record inserted correctly into DB and at the second time, I reach an error: duplicate entry at..... because record has been insert at the first time. How can I prevent the second call? Or better: why this second call happen?
Kind regards,
Hello and welcome to the forums [member=intysaez].
Please share your controller's code so we can help you.
Posted 27 June 2013 - 12:58 PM
Hello and welcome to the forums [member=intysaez].
Please share your controller's code so we can help you.
Thank!! The controller code is quite simple.
function set_inserted_data($post_array, $primary_key) {
if (!filter_var($post_array['email'], FILTER_VALIDATE_EMAIL)){
echo json_encode(array('success' => false , 'error_message' =>lang('no_valid_email')));
die();
}else{
$post_array['passw'] = md5($post_array['passw2']);
unset($post_array['passw2']);
return $post_array;
}
}
This is the function in callback_before_insert. The main problem is that this function is call twice and therefore duplicate entry key error happen. I change the callback (from beforeinsert to insert) but the problem persit.
Regards,
Posted 28 June 2013 - 04:38 AM
Please post the controller's code from where you call this callback function.
This is the function in callback_before_insert.
Posted 24 August 2015 - 19:14 PM
Hi,
I have a similar problem.
I want to put the id key in a public variable before or after insert to redirect the page after insert record.
This is my code.
$crud->callback_before_insert(array($this,'get_id')); $crud->set_lang_string('insert_success_message', 'Your data has been successfully stored into the database.<br/>Please wait while you are redirecting to the list page. <script type="text/javascript"> window.location = "'.site_url(strtolower('fact').'/'.strtolower('viewCRUD/add/').$idrir).'"; </script> <div style="display:none"> '); $output = $crud->render(); $this->_example_output($output); } public function get_id($post_array){ $idrir = $post_array['RIRKey'] ; return $post_array; }
Please help me !!!!