简体   繁体   中英

Codeigniter query builder class results

Is it safe to assume in codeigniter query builder class for every successful query it will automatically return true and false if it fails?

If so, does it apply to CRUD? or is it only true when inserting and deleting data? For updating would i have to check affected rows? or will it also return a true result if update was successful?

For example:

controller:

$data = array('username' => $this->input->post('username'),
    'password' => password_hash($this->input->post('password'), PASSWORD_BCRYPT),
    'date_created' => mdate('%Y-%m-%d', time())
);

$result = $this->account_model->create($data);

if($result == TRUE){
    $this->session->set_flashdata('message', 'Registration Successful');
    redirect('pages/login');
}else{
    $this->session->set_flashdata('message', 'Registration Failed');
    redirect('pages/register');
}

model:

public function create($new_user){
    $insert = array('username' => $new_user['username'],
        'password' => $new_user['password'], 
        'date_created' => $new_user['date_created']
    );

    $result = $this->db->insert('user', $insert);

    return $result;

}

also, is it bad if i returned the query statement itself? like,

return $this->db->insert('user', $insert);

No. Not really. You can, however, control what the model returns. Keep in mind that an empty resultset, for example, is not necessarily a "failed" query. There's a lot of use cases where an empty resultset may be a good thing.

After actually running the query you will get an object with the result

for instance, when selecting, you can do $result = $this->db->get() and your $result variable will become an object with a lot of information you can access:

Number of returned rows: $result->num_rows(); Returned contents: $result->result(); Specific rows: $result->row(0); Specific field in a specific row: $result->row(3)->field_name;

When inserting or updating, $this->db->affected_rows() becomes available so you can check how many rows were updated or inserted. Inserts also make $this->db->insert_id() available in case you need the table's primary key value for the row you inserted.

Thus, you are in control of the logic.

when selecting, I usually do:

$query = $this->db->get();
if ($query->num_rows() == 0)
{
  return false;
}

else
{
  return $query->row(0);
}

Then I check, in the controller, if the model returned false or a real resultset (sometimes I just return true instead of the actual resultset if I'm just checking for something to exist). You can tailor the results to your needs.

When inserting, I usually check:

if ($this->db->affected_rows() != 0)
{
  return $this->db->insert_id();
}

else
{
  return false;
}

(in the controller, a false being returned would be handled as an "insertion failed").

To answer your question more globally: No, nothing really happens by default, but Codeigniter gives you everything you need to tailor the behavior of your site to your specific needs and tastes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM