Welcome Guest, Not a member yet? Register   Sign In
Easy way to get form data into array ready to be persisted
#1

[eluser]welzie[/eluser]
I am looking for a way to avoid manually getting all of the form values from $_POST and sticking them into an array so it can be passed to the model.

Current process:
Code:
//controller code that builds and persists $data array
class User extends MY_Controller {
function persist() {
     $data['email'] = $_POST['email'];
     $data['name'] = $_POST['name'];
     $data['distributorId'] = $_POST['distributorId'];
     $this->Some_model->insert($data);
}

Code:
//model code that persists $data array
class Some_model extends CI_Model {
     function insert($data) {
$this->db->insert($this->tableName, $data);
     }
     function update($id, $data) {
$this->db->update($this->tableName, $data, 'id = '.$id);  
     }
}


My idea:
Code:
//Controller code uses utility method to copy all values with the same name
//from one array to the another. This would be a method added to MY_Controller.
class User extends MY_Controller {
function persist() {
     $targetFields = array('email', 'name', 'distributorId');
     $data = $this->copyCommonEntries($_POST, $targetFields);
     $this->Some_model->update($data);
}

$data would end up with an array with all the indices from $targetFields, but would have the value from $_POST for the matching index.

Is there already something that does this?




Theme © iAndrew 2016 - Forum software by © MyBB