[eluser]JonoB[/eluser]
Hi all,
I am (almost) brand new to MVC frameworks, and I'm just trying to get my head around the best way to handle edits and updates. I've set up a simple test to CRUD a database. The one part that I am not 100% sure is how I have handled the edit and update logic.
I am using
www.phpactiverecord.org as my ORM, as I am familiar with it from another project. I may switch over to CI ActiveRecord lib in due course.
I kinda cobbled this code from the help docs - its part of the products controller. Basically, the edit function and view lets me look at a single product. Clicking submit calls the update function, which runs through the validation rules. If validation passes, then we update the database and redirect to the product listing. If validation fails, then we redirect back to the edit view so that the user can try again.
Code:
function edit($id = '')
{
if ($id)
{
$data['product'] = product_model::find($id)->to_array();
$data['message'] = '';
}
else
{
$data['message'] = 'No product found';
}
$data['title'] = 'Edit Product';
$data['action'] = site_url('products/update');
$data['link_back'] = anchor('products', 'Back to list of products', array('class' => 'back'));
$this->load->view('products/edit', $data);
}
function update()
{
// set validation properties
$this->form_validation->set_rules('code','Product Code','required|max_length[10]');
$this->form_validation->set_rules('qty_sales','Sales Qty','numeric');
$this->form_validation->set_rules('qty_purchases','Purchases Qty','numeric');
$this->form_validation->set_rules('description','Description','max_length[255]');
$this->form_validation->set_rules('unit_sales','Unit Sales','numeric');
$this->form_validation->set_rules('unit_purchases','Unit Purchases','numeric');
$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
$data = array(
'id' => $this->input->post('id'),
'code' => $this->input->post('code'),
'description' => $this->input->post('description'),
'qty_sales' => $this->input->post('qty_sales'),
'qty_purchases' => $this->input->post('qty_purchases'),
'unit_sales' => $this->input->post('unit_sales'),
'unit_purchases' => $this->input->post('unit_purchases'));
// run validation
if ($this->form_validation->run() == FALSE)
{
$ret['title'] = 'Edit Product';
$ret['action'] = site_url('products/update');
$ret['link_back'] = anchor('products', 'Back to list of products', array('class' => 'back'));
$ret['message'] = '';
$ret['product'] = $data;
$this->load->view('products/edit', $ret);
}
else
{
$product = product_model::find($this->input->post('id'));
$product->update_attributes($data);
redirect('/products', 'refresh');
}
}
So, this all seems to work fine, but I have no idea if this follows 'good' practise or the conventions.
Thanks for any advice that you can give.