⚠ 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 before insert and update not running



particleandparcel
  • profile picture
  • Member

Posted 08 July 2013 - 16:39 PM

Hi all,

 

I've been trying to figure out all morning why my callbacks before inserting and updating don't appear to be doing anything. The fields are visible and editable, but I wouldn't think that would be an issue. Whether they're left blank or something is filled in, nothing is ever set.

 

I'd be happy if I could figure out how to debug the $post_array but I can't figure out how to do that, because the call is done in Ajax.

 

FWIW I'm using Ion Auth. Otherwise this is a stock installation of GC and CI.

 

The controller has:

public function jobpostings($filter = false) {
	$filtercriteria = ($this->input->get("filter")) ? array("filter" => $this->input->get('filter'), "criteria" => $this->input->get('criteria')) : false;
	// Set up table
	$this->grocery_crud->set_table('jobpostings');
	$this->grocery_crud->set_subject("Job Posting");
	$this->grocery_crud->columns("JobStatus", "Community", "Dept", "JobTitles", "FullPartTime", "Shift", "Comments");
	$this->grocery_crud->display_as("JobTitles", "Job Title");
	$this->grocery_crud->display_as("FullPartTime", "FT / PT");
	$this->grocery_crud->display_as("Dept", "Department");
	$this->grocery_crud->display_as("SendResumeTo", "Send Resume To:");
	$this->grocery_crud->order_by("Community");
	
	if ($filter == "open") {
		$this->grocery_crud->where('JobStatus', '1');
	} else if ($filtercriteria) {
		$this->grocery_crud->where($filtercriteria['filter'], $filtercriteria['criteria']);
	}
	
	// Set relations
	$this->grocery_crud->set_relation('LastModifiedBy', 'hradm_users', 'username', "username != 'administrator'");
	$this->grocery_crud->set_relation('AddedBy', 'hradm_users', 'username', "username != 'administrator'");
	
	// Set up fields we actually want to edit
	$this->grocery_crud->fields("JobStatus", "Community", "Dept", "JobTitles", "FullPartTime", "Shift", "Openings", "SendResumeTo", "Comments", "AddedOn", "AddedBy", "LastModified", "LastModifiedBy");
	$communities = $this->_callback_getlist("Community", "jobcommunities");
	$depts = $this->_callback_getlist("Dept", "jobdepts");
	$jobtitles = $this->_callback_getlist("JobTitles", "jobtitlesdescriptions");
	$jobstatus = array("1" => "Open", "0" => "Closed");
	
	$this->grocery_crud->field_type("FullPartTime", "dropdown", array("Full Time" => "Full Time", "Part Time" => "Part Time", "Both" => "Both"));
	$this->grocery_crud->field_type("Shift", "dropdown", array("N/A" => "N/A", "Day" => "Day", "Afternoon" => "Afternoon", "Night" => "Night", "On-Call" => "On-Call", "Weekend Shifts" => "Weekend Shifts", "All Shifts" => "All Shifts"));
	$this->grocery_crud->field_type("SendResumeTo", "dropdown", $communities);
	$this->grocery_crud->field_type("Community", "dropdown", $communities);
	$this->grocery_crud->field_type("Dept", "dropdown", $depts);
	$this->grocery_crud->field_type("JobTitles", "dropdown", $jobtitles);
	$this->grocery_crud->field_type("JobStatus", "dropdown", $jobstatus);
	$this->grocery_crud->field_type("AddedOn", "datetime");
	$this->grocery_crud->field_type("LastModified", "datetime");
	
	// inserts and update callbacks
	$this->grocery_crud->callback_before_insert(array($this, '_create_callback'));
	$this->grocery_crud->callback_before_update(array($this, '_modified_callback'));
	
	$output = $this->grocery_crud->render();
	
	$this->_output_view($output, "jobpostings", array("filters_communities" => $communities, "filters_depts" => $depts));
} 

And then the callbacks (FWIW, I verified that the $user->id is set correctly):

function _created_callback($post_array) {
	$user = $this->ion_auth->user()->row();
	$post_array['AddedBy'] = $user->id;
	$post_array['AddedOn'] = date('Y-m-d H:i:s');
	$post_array['LastModified'] = date('Y-m-d H:i:s');
	return $post_array;
}

function _modified_callback($post_array) {
	$user = $this->ion_auth->user()->row();
	$post_array['LastModifiedBy'] = $user->id;
	$post_array['LastModified'] = date('Y-m-d H:i:s');
	return $post_array;
}

None of these fields are set when I look in the database-- they're all NULL. The purpose is to auto-log creation and modification times, while (at the same time) keeping users from having to remember to enter it.

 

Eventually they'll be set to read-only for edits, and will only be manually settable on insertsw, but first I'd have to feel confident this was working.

 

Does anyone have any suggestions?

 

Thanks!


Amit Shah
  • profile picture
  • Member

Posted 08 July 2013 - 19:14 PM

Hi there,

 

Well if i am not mistakened, this fields that you want to update are the ones that you don't want user to fill in but you want to store it automatically @the time of fill up. Insertion exclusively.. Then why don't you change the field type to be hidden. As for AddedBy  can directly be set to the $user->id in the crud function itself by field_type function call setting it as default value. Rest of the fields, will recommend setting it as hidden as it aint necessary for user to see it / modify / alter them. 

 

As for add you can define the fields you want to make GC set while inserting by calling $gc->add_fields function and the same while editing, you dont need to have addedby and addedon fields, so dont define them and GC wont set them while updating the record.

 

This should solve your issue .. it seems. The code dose not have much mistake / issue as why the above described pattern should fail. 


particleandparcel
  • profile picture
  • Member

Posted 09 July 2013 - 12:01 PM

That's exactly right. Did the trick. Leaving the fields exposed meant that I couldn't override them on insert or update, but once I switched them to invisible fields, I had no problem at all.
 
I wish I could figure out how to expose them in some way so the HR director could review the listings, but that'll clearly require more code. I could probably fetch those fields and just insert them outside the Grocery CRUD table, or write a simple view function.
 
Thank you for your help!

davidoster
  • profile picture
  • Member

Posted 09 July 2013 - 14:14 PM

 

I wish I could figure out how to expose them in some way so the HR director could review the listings, but that'll clearly require more code. I could probably fetch those fields and just insert them outside the Grocery CRUD table, or write a simple view function.
 

 

 

Use an auth library and give them access to the fields you want depending on the id or group assignment. This will make the whole thing very easy to implement.