Welcome Guest, Not a member yet? Register   Sign In
Best way for checking true result
#1

[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! :-)
#2

[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');
}
#3

[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
#4

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

#5

[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();
#6

[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);
#7

[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)
);
#8

[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?
#9

[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?

#10

[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! :-)




Theme © iAndrew 2016 - Forum software by © MyBB