Welcome Guest, Not a member yet? Register   Sign In
Two consecutive database queries using Active Record
#1

[eluser]Unknown[/eluser]
I have two different type of queries that need to be done consequtively in a model method.

Code:
$this->db->select('user.id, userOptions.available')
$this->db->from('user');
$this->db->join('userOptions', 'user.id = userOptions.userId', 'left outer');
$this->db->where('user.code', $code);

// use the $this->db->result() here
//
// (...)

in the continuation of the method I want the id taken from the first query to be used in an update context.

Code:
$data = array('name' => $name);

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

What i'd like to know is how to reset the query's WHERE part and all other things between the calls to the different types of queries? How does the db object know that the $this->db->where('user.code', $code) from the first query is not used as a where constraint in second query? How to correctly approach this scenario?
#2

[eluser]CroNiX[/eluser]
Why not just:
Code:
$this->db->select('user.id, userOptions.available')
$this->db->join('userOptions', 'user.id = userOptions.userId', 'left outer');
$this->db->where('user.code', $code);
$this->db->get('user');
$user_data = $this->db->row_array();

//this query will have nothing to do with the last query, because the last query has already been executed.
//So all "wheres", "joins" etc, are reset now.
$data = array('name' => $name);

$this->db->where('id', $user_data['id']);
$this->db->update('user', $data);

However, if you check at the bottom of the Generating Query Results page in the manual, you will see you can also do:
$user_data->free_result();
which will actually close that db resource connection and reset everything, freeing up memory. But it is mostly unnecessary unless dealing with massive data.




Theme © iAndrew 2016 - Forum software by © MyBB