How to pass parameter $where to update function built in code igniter 2.1 in CI_DB_active_record class

StackOverflow https://stackoverflow.com/questions/21397102

  •  03-10-2022
  •  | 
  •  

質問

I have a record which should be updated using update function built in code ignitor 2.1. so rather than defining where close separately, I want to pass it like a variable $this->db->update($table, $record,$where); (I know this is built in functionality but all the example follows in two step manner, using where function and using update function).

$this->db->where('id','3');
$this->db->update('data', $record);

Should I create a separate where function and return $where value in-order to keep MVC pattern clear

function where($field,$value)

and

function update($able,$record,$where)

how to achieve this?

役に立ちましたか?

解決 2

You can use two methods like you mentioned to get more object oriented. Then you will be able to use that function to update any table record.

and also the above answer is correct as well.

$this->db->update('mytable', $data, array("id" => $id));

他のヒント

You can do in any of below methods:

Method 1: You can pass condition as string in third parameter

$this->db->update('mytable', $data, "id = $id");

Method 2: You can pass condition as array in third parameter

$this->db->update('mytable', $data, array("id" => $id));

Method you mentioned in your question is also correct.

$this->db->where('id','3');
$this->db->update('data', $record);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top