简体   繁体   English

Codeigniter查询构建器类结果

[英]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? 在codeigniter查询构建器类中为每个成功的查询假设是否安全,如果失败,它将自动返回true和false?

If so, does it apply to CRUD? 如果是这样,它是否适用于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: 例如,在选择时,你可以执行$result = $this->db->get() ,你的$result变量将成为一个包含大量信息的对象:

Number of returned rows: $result->num_rows(); 返回的行数: $result->num_rows(); Returned contents: $result->result(); 返回内容: $result->result(); Specific rows: $result->row(0); 特定行: $result->row(0); Specific field in a specific row: $result->row(3)->field_name; 特定行中的特定字段: $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. 插入或更新时, $this->db->affected_rows()变为可用,因此您可以检查更新或插入的行数。 Inserts also make $this->db->insert_id() available in case you need the table's primary key value for the row you inserted. 插入还可以使$this->db->insert_id()可用,以防您需要为所插入的行输入表的主键值。

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). 然后我在控制器中检查模型是否返回false或实际结果集(有时我只返回true而不是实际的结果集,如果我只是检查存在的东西)。 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"). (在控制器中,返回的false将被视为“插入失败”)。

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 更全面地回答您的问题:不,默认情况下没有任何事情发生,但Codeigniter为您提供了根据您的特定需求和品味定制网站行为所需的一切

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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