Welcome Guest, Not a member yet? Register   Sign In
How to convert this update QUERY to ActiveRecord
#1

[eluser]MpaK69[/eluser]
I have this query
Code:
$this->db->query('UPDATE '.$this->dbprefix.'menu SET ord_id = ord_id-1 WHERE ord_id > ?', $row['ord_id']);

but I want to make this with ActiveRecord

some think like this

Code:
$this->db->set('ord_id = ord_id-1');
$this->db->where('ord_id >', $row['ord_id']);
$this->db->update('menu');

can some one help with this problem?
#2

[eluser]Christopher Blankenship[/eluser]
First you have to of course obtain the $row['ord_id'] and then this is what I believe you are looking for:
Code:
$data = array(
   'ord_id' => ($row['ord_id'] - 1),
   );
$this->db->where('ord_id >', $row['ord_id']);
$this->db->update('menu', $data);
Basically this will update the field subtracting 1 from it each time it is accessed.
#3

[eluser]MpaK69[/eluser]
nop Sad
if I have now more than 2000 records! If I will do it in cycle that will be tragedy
#4

[eluser]Christopher Blankenship[/eluser]
Yeah with that many records that would not work very well. Oh i think i understand what you are looking for now you want it to actively update that fields ord_id subtracting 1 from it for anything greater than the supplied row ord_id.

For example
here would be 3 ord_id fields in the db.
127
145
198

and the supplied row ord_id was 129.

127 would not be touched while
145 would be updated to 144
and 198 would be updateed to 197
#5

[eluser]MpaK69[/eluser]
yeah, ok, I understand I cant do this with AC pattern

I did it with query

Code:
if($neword < $oldord){
            // UP
            $this->db->query('UPDATE '.$this->dbprefix.'menu SET ord_id=ord_id+1 WHERE ord_id >= ? AND ord_id <= ? AND pid = ?', array($neword, $oldord, $pid) );
            $this->db->set('ord_id', $neword);
            $this->db->where('uid', $lastid);
            $this->db->update('menu');
        }elseif($neword > $oldord){
            // DOWN
            $this->db->query('UPDATE '.$this->dbprefix.'menu SET ord_id=ord_id-1 WHERE ord_id <= ? AND ord_id >= ? AND pid = ?', array($neword, $oldord, $pid) );
            $this->db->set('ord_id', $neword);
            $this->db->where('uid', $lastid);
            $this->db->update('menu');
        }




Theme © iAndrew 2016 - Forum software by © MyBB