Kango
Kango

Reputation: 847

format to write query in codeigniter

I am new with codeigniter.

I have following query, each query having different table name and different where condition.

Can i make this query in single query or is it correct way to execute query. i have to execute all this query.

$q = $this->db->where('id', $st)->update('st_info', array(
    'status' => 0,
    'updated_time' => $this->info_model->getTime(),
    'updated_ip' => $this->info_model->getIP()
));

$q = $this->db->where('verid', $vid)->update('st_version', array(
    'status' => 0,
    'updated_time' => $this->info_model->getTime(),
    'updated_ip' => $this->info_model->getIP()
));

$q = $this->db->where('id', $id)->update('st_raw', array(
    'status' => 0,
    'updated_time' => $this->info_model->getTime(),
    'updated_ip' => $this->info_model->getIP()
));

Upvotes: 0

Views: 119

Answers (4)

Muhammad Raheel
Muhammad Raheel

Reputation: 19882

Create a single function

public function update($table,$where,$data)
{
    $this->db->where($where);
    $this->db->update($table,$data);
}   

Now call this function like this from controller

function update_call()
{
    $data['status']         =   0;
    $data['updated_time']   =   $this->info_model->getTime();
    $data['updated_ip']     =   $this->info_model->getIP();

    $where['id']    =   $st;
    $table          =   'st_info';

    $this->model_name->update($table,$where,$data);

    unset($where);
    unset($table);

    $where['verid'] =   $vid;
    $table          =   'st_version';

    $this->model_name->update($table,$where,$data);

    unset($where);
    unset($table);

    $where['id']    =   $id;
    $table          =   'st_raw';

    $this->model_name->update($table,$where,$data);     
}

Upvotes: 0

Rahul Chipad
Rahul Chipad

Reputation: 2401

Try this:

$data=array(
      'status' => 0,
      'updated_time'  => $this->info_model->getTime(),        
      'updated_ip' => $this->info_model->getIP()                                                
)); 

 $this->db->where('id',$st);
 $this->db->update('st_info', $data); 

 $this->db->where('verid',$vid);
 $this->db->update('st_version', $data); 

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

Upvotes: 0

Paulraj
Paulraj

Reputation: 3397

Since you are using same columns for all the tables, i prefer to work with below method,

    $data = array(
    'status' => 0,
    'updated_time' => $this->info_model->getTime(),
    'updated_ip' => $this->info_model->getIP()
);

    $q = $this->db->where('id', $st)->update('st_info', $data);
    $q = $this->db->where('verid', $vid)->update('st_version', $data);
    $q = $this->db->where('id', $id)->update('st_raw', $data);

Upvotes: 2

Danny
Danny

Reputation: 1185

That is correct you cannot combine all that into a single query.

Upvotes: 0

Related Questions