[eluser]Unknown[/eluser]
Here's a very simple solution. I put this in a helper file. In my controller, my "new" function submits the would-be database object "$person" as a null value:
Code:
function new_person()
{
$data['person'] = null;
...
$this->load->view('person_edit', $data);
}
Whereas it submits the person values from the database if it is the edit request
Code:
function edit_person()
{
...
$data['person'] = $this->person_model->fetch_person( $kPerson );
...
$this->load->view('person_edit', $data);
}
The helper function is this:
Code:
function getValue($object, $item){
$output = "";
if($object){
$output = $object->$item;
}
return $output;
}
In the form, then the print out would be something like this:
Code:
...
echo form_input('firstName', getValue($person, 'firstName'));
...
The one catch, which I think could be resolved, is if you type a non-existent property as the "item" parameter for the getValue function. It seems best to have this remain error-producing because you'd want the feedback. If you really wanted to prevent errors from being displayed, you could use:
Code:
$var_list = get_object_vars($object);
$var_keys = array_keys($var_list);
//produces an associative array of object properties
if (in_array($item, $var_keys)){
$output = $object->$item;
}
I just whipped that one off. It works, but seems inelegant. You could just work with the $var_list array using the larger number of array-manipulation and validation tools available for php to extract the data you want.
You could also add a third function parameter to getValue that allows a default return value