CodeIgniter Forums
Best way for checking true result - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Best way for checking true result (/thread-51507.html)

Pages: 1 2


Best way for checking true result - El Forum - 05-07-2012

[eluser]someone Smile[/eluser]
Hello,

What's the best way for checking if some method in model or anywhere else was correctly carried out?

Is this a good way?

Model:
Code:
$data['field1'] = $this->input->post('field1');
$data['field2'] = $this->input->post('field2');
$data['field3'] = $this->input->post('field3');

if ($this->db->insert('table', $data))
{
   return TRUE;
}
else
{
   return FALSE;
}

Controller:
Code:
if ($this->form_validation->run() == FALSE)
{
    $this->load->view('page_view', $data);
}
else
{
    if ($this->Model->Insert_data())
    {
       $this->session->set_flashdata("insertsuccess", TRUE);
    }
    else
    {
       $this->session->set_flashdata("inserterror", TRUE);
    }
    $this->load->view('page_view', $data);
}

Thanks for answers! :-)


Best way for checking true result - El Forum - 05-07-2012

[eluser]csotelo[/eluser]
If you are going to use flash_data in response, you can save the response in the controller:

Model:
Code:
$data['field1'] = $this->input->post('field1');
$data['field2'] = $this->input->post('field2');
$data['field3'] = $this->input->post('field3');

if ($this->db->insert('table', $data))
{
   $this->session->set_flashdata("insertsuccess", TRUE);
}
else
{
   $this->session->set_flashdata("inserterror", TRUE);
}

Controller:
Code:
if ($this->form_validation->run() == FALSE)
{
    $this->load->view('page_view', $data);
}
else
{
    $this->Model->Insert_data();
    $this->load->view('page_view');
}



Best way for checking true result - El Forum - 05-07-2012

[eluser]Stefan Hueg[/eluser]
And don't use $this->input->post() in your models. Keep them clean and put them in your controller, passing the data to your model.

That's the MVC way Smile


Best way for checking true result - El Forum - 05-07-2012

[eluser]InsiteFX[/eluser]
On an insert you can test it by returning
Code:
return $this->db->insert_id();




Best way for checking true result - El Forum - 05-08-2012

[eluser]someone Smile[/eluser]
[quote author="Stefan Hueg" date="1336433044"]And don't use $this->input->post() in your models. Keep them clean and put them in your controller, passing the data to your model.

That's the MVC way Smile[/quote]

Could you please show me this on an example. Thanks!

[quote author="InsiteFX" date="1336447459"]On an insert you can test it by returning
Code:
return $this->db->insert_id();
[/quote]

Is this good then?
Code:
$data['field1'] = $this->input->post('field1');
$data['field2'] = $this->input->post('field2');
$data['field3'] = $this->input->post('field3');
$this->db->insert('table', $data);

return $this->db->insert_id();



Best way for checking true result - El Forum - 05-08-2012

[eluser]Stefan Hueg[/eluser]
Model:
Code:
function my_insert($data)
{
    $this->db->insert('table', $data);
    return $this->db->insert_id();
}

Controller:
Code:
$data = array(
    'field_1' => $this->input->post('field1'),
    'field_2' => $this->input->post('field2'),
    'field_3' => $this->input->post('field3')
);
$insert_id = $this->model_name->my_insert($data);



Best way for checking true result - El Forum - 05-08-2012

[eluser]InsiteFX[/eluser]
You should be cleaning the input data using xss_clean! That is what the TRUE doe's
Code:
$data = array(
    'field_1' => $this->input->post('field1', TRUE),
    'field_2' => $this->input->post('field2', TRUE),
    'field_3' => $this->input->post('field3', TRUE)
);



Best way for checking true result - El Forum - 05-08-2012

[eluser]someone Smile[/eluser]
Now I'm little confused with all this.

Model:
Code:
function my_insert($data)
{
    $this->db->insert('table', $data);
    return $this->db->insert_id();
}

Controller:
Code:
$data = array(
    'field_1' => $this->input->post('field1', TRUE),
    'field_2' => $this->input->post('field2', TRUE),
    'field_3' => $this->input->post('field3', TRUE)
);
$insert_id = $this->model_name->my_insert($data);

if ($insert_id == TRUE)
{
    $this->session->set_flashdata("insertsuccess", TRUE);
}
else
{
    $this->session->set_flashdata("inserterror", TRUE);
}

If I have xss_clean property in form validation setup (for all fields), do I have to add TRUE at the end of input->post?


Best way for checking true result - El Forum - 05-08-2012

[eluser]cartalot[/eluser]
> If I have xss_clean property in form validation setup (for all fields), do I have to add TRUE at the end of input->post?

well you didn't include that in yr code -- but now i'm wondering this as well.
is it 'best practice' to just always have TRUE in input post (for xss clean) as a double check?




Best way for checking true result - El Forum - 05-27-2012

[eluser]someone Smile[/eluser]
I have one more question. Is this way good or it can be better then this?

Model:
Code:
function my_insert($data)
{
    if ($this->db->insert('table', $data))
    {
        return $this->db->insert_id();
    }
}

Controller:
Code:
$data = array(
    'field_1' => $this->input->post('field1', TRUE),
    'field_2' => $this->input->post('field2', TRUE),
    'field_3' => $this->input->post('field3', TRUE)
);

if ($this->model_name->my_insert($data) == TRUE)
{
    $this->session->set_flashdata("insertsuccess", TRUE);
}
else
{
    $this->session->set_flashdata("inserterror", TRUE);
}

Thanks! :-)