Welcome Guest, Not a member yet? Register   Sign In
Active record class - update
#1

[eluser]vbnullchar[/eluser]
how can i do this in active record

Code:
"update table set field1 = field1 + 10";

heres what i started but not working

Code:
$data = array( "field1" => "field1 + 10");
$this->db->update('table1', $data);
#2

[eluser]vbnullchar[/eluser]
please ignore this post...
#3

[eluser]Lone[/eluser]
Did you find a solution or did you find out that this is currently not supported? We ran into the same problem a couple of days as well.
#4

[eluser]vbnullchar[/eluser]
theres is a solution in the documentation. heres my working code

Code:
$query = "update leavecredits set $field = $field + $less ";
$query.= "where month= ? and year= ? and rosterId= ?";
    
$this->db->query($query, array($month,$year,$rosterId));
#5

[eluser]Lone[/eluser]
Ahh yup thats the same way we went - just a raw query as such instead of activerecord.

Would be nice to have a simple active record function to do it but its no big requirement Wink
#6

[eluser]xwero[/eluser]
Reading Dereks forum posts about this topic the people at Ellislab haven't found the right solution to deal with raw query parts yet. I think the main obstacle is the way the AR library lets you add parts to the query. You can choose between an array or a key value pair for the build methods and you can add a part of the query as array to the action method.
Code:
// most code
$this->db->set('field1','value1');
$this->db->set('field2','value2');
$this->db->where('field3','value3');
$this->db->where('field4','value4');
$this->db->update('table');
// alternative 1
$setarr = array('field1'=>'value1','field2'=>'value2');
$this->db->set($setarr);
$wherearr = array('field3'=>'value3','field4'=>'value4');
$this->db->where($wherearr);
$this->db->update('table');
// alternative 2
$setarr = array('field1'=>'value1','field2'=>'value2');
$wherearr = array('field3'=>'value3','field4'=>'value4');
$this->db->where($wherearr);
$this->db->update('table',$setarr);
So there are a lot of possibilities they have to cover not to break the way developers are using these methods.

At first Derek added a boolean to the set method as the third parameter
Code:
$this->db->set('field1','value1',false); // doesn't escape the value
Which would be a nice solution i haven't seen the code to handle escaping an array value
Code:
$setarr = array('field1'=>array('value1',false),'field2'=>'value2');
This could be an option but what if the value is an array? The safest way for an array would be
Code:
$setarr = array('field1'=>array('value'=>'value1','escape'=>false),'field2'=>'value2');
But that is not really developer friendly and it would involve a lot of checking.

His next train of thought was adding a new method raw_where, i guess a raw_set method would be the next logical step. This method doesn't escape the value and i think this is a better solution but i read in one of the threads the method is removed again. It's a good solution because it doesn't break the current way people use the already existing queries and if the method is build the same way it will be possible to compact the code if there are multiple fields that have to be added.

But maybe they still have some tricks up their sleeve Smile
#7

[eluser]vbnullchar[/eluser]
Thanks alot xwero Smile




Theme © iAndrew 2016 - Forum software by © MyBB