Welcome Guest, Not a member yet? Register   Sign In
Second active record query before first has executed? Overlapping queries
#1

[eluser]Ninjabear[/eluser]
Hi. I'm trying to do an update query but also do another query in the middle but can't work it out. It's necessary for me to do it in the middle as its dependent on the value of c_id in the if statement.

I did do searches for this problem but couldn't find anything except parallel queries which seemed to be something different.

Code:
if(isset($options['name']))
    $this->db->set('name',$options['name']);
      
  //Alter customer.
  if(isset($options['c_id']))
  {
    $this->db->set('c_id',$options['c_id']);
    
    //Do overlapping query here
    $this->db->set('c_id',$options['c_id']);
    $this->db->where('p_id',$options['p_id']);
    $task = $this->db->update('time');    
  }

  $this->db->update('projects');

So is there a way of executing queries before the first has been completed?
#2

[eluser]john_j[/eluser]
I don't think you can stop the execution of a query in the middle. Maybe you are looking for "transactions". http://ellislab.com/codeigniter/user-gui...tions.html
#3

[eluser]Ninjabear[/eluser]
[quote author="john_j" date="1346860427"]I don't think you can stop the execution of a query in the middle. Maybe you are looking for "transactions". http://ellislab.com/codeigniter/user-gui...tions.html[/quote]

I don't think I want to stop the query in the middle, I just want to run another query in its entirety before the first one has been run. In other words postpone the first query for a while without the from / where / group_by statements getting mixed up with the new query.

So something like:

Code:
$this->db->set('name',$options['name']);

  $my_update->db->set('p_id',$options['p_id']);
  $my_update->db->where('t_id',$options['t_id']);

  $my_update->db->get('times');

$this->db->get('times');

BTW I'm using MyIsam tables but not sure transactions is what I'm looking for.
#4

[eluser]john_j[/eluser]
In that case you will have to use 2 separate queries. Run the second query after the first one has completed.

Regarding the from,group_by etc getting mixed up - you need to use separate variables so that they don't mix up.
#5

[eluser]Ninjabear[/eluser]
[quote author="john_j" date="1346863289"]In that case you will have to use 2 separate queries. Run the second query after the first one has completed.

Regarding the from,group_by etc getting mixed up - you need to use separate variables so that they don't mix up.[/quote]

It isn't a problem if I run them one after the other! It's if I want to run another query in the middle. Can I use things like:

Code:
$my_update->db->get();
#6

[eluser]Aken[/eluser]
update() will run a query with all current set parameters, and then reset active record, removing anything that has been set. You can't do something "in the middle". You have to run one query, and then build and run a second.
#7

[eluser]LevZabudko[/eluser]
After years now:
I've changed class CI_DB_active_record, so now you can build and execute new queries while prepearing another. Here is how:
https://github.com/EllisLab/CodeIgniter/...el-queries




Theme © iAndrew 2016 - Forum software by © MyBB