Jump to content


Callback before/after insert/update help plz

callbacks insert update

  • Please log in to reply
8 replies to this topic

#1 DRKgatsby

DRKgatsby

    Member

  • Members
  • PipPip
  • 15 posts

Posted 07 June 2012 - 07:01 PM

OK, so I'm using grocery_CRUD v1.8, I think, along with CodeIgniter v2.1 and I'm really not understanding how the callback function for grocery crud work. How do I know if I should use the callback before insert verse after insert, and same for update. Any time I try using after update or insert the crud edit table will endlessly load and once I click cancel it will leave an empty row in the database.


Here is my controller:

 

$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/ent...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);




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:


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');
}



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;
}


Can anyone help me out with this?

#2 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,137 posts
  • LocationLondon

Posted 07 June 2012 - 09:26 PM

Yes you have to add the fields at your fields functions so you have to add it like this:

$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 Image

#3 DRKgatsby

DRKgatsby

    Member

  • Members
  • PipPip
  • 15 posts

Posted 08 June 2012 - 03:40 PM

Thank you very much. That helps me understand a bit more now how to use callbacks =)

I also found that doing an update after the insert works as well

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);
}


#4 DRKgatsby

DRKgatsby

    Member

  • Members
  • PipPip
  • 15 posts

Posted 08 June 2012 - 04:06 PM

I have come across a new problem in the function after that. I understand that I cannot pass a parameter through the callback functions in grocery crud, but is there a work around so I can bring a parameter from one function to another?

In the controller I posted before I have an add_action function that calls the next controller and passes the $account_id like so:


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/ent...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);
}


My crud table does the same thing
So before the views get loaded I save the $account_id in the user table like so:

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'];
}




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.


 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);
}



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);
}


Does it have to do with the session information? If so, how would I go about retrieving the data?

#5 intysaez

intysaez

    Newbie

  • Members
  • Pip
  • 2 posts

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,



#6 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

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 intysaez.

Please share your controller's code so we can help you.


____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr


#7 intysaez

intysaez

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 27 June 2013 - 12:58 PM



Hello and welcome to the forums 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,



#8 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

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. 


____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr


#9 fifiricky

fifiricky

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 24 August 2015 - 07: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 !!!!







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users