EllisLab text mark
Advanced Search
     
Increment field with ActiveRecord
Posted: 12 October 2007 10:51 AM
Joined: 2007-08-09
52 posts

It seems that with ActiveRecord it is impossible to increment the value of a field without fetching it first.

Here’s what I’d like to do:

$this->db->where('ThreadId'$ThreadId);
$this->db->set('ViewCount = ViewCount + 1');
$this->db->update('Forum_Threads'); 

But that doesn’t work, the resulting query looks something like this:

UPDATE Forum_Threads SET 'ViewCount = ViewCount + 1' '' WHERE ThreadId 

Is there anyway, with ActiveRecord, to increment a field, without fetching it’s value first ? I don’t need to know the value at all, so SELECTing it first would be a waste of resources.

Do I have to resort to simple_query() ?

 
Posted: 08 January 2008 08:37 AM   [ # 1 ]   [ Rating: 0 ]
Joined: 2007-10-16
63 posts

*bump* - I very much have the same problem. Is there a solution?

 
Posted: 08 January 2008 09:02 AM   [ # 2 ]   [ Rating: 0 ]
Joined: 2006-07-14
4237 posts

I reported a bug for the same problem. Derek said it is fixed in the svn and you have to use (update : it’s still not in the svn code)

$this->db->set('ViewCount','ViewCount + 1',false); 

The thirth argument is for escaping the value that has been set to true by default.

The code for the new set method is

function set($key$value '',$escape true)
    
{
        $key 
$this->_object_to_array($key);
    
        if ( ! 
is_array($key))
        
{
            $key 
= array($key => $value);
        
}    

        
foreach ($key as $k => $v)
        
{
            $this
->ar_set[$k] = ($escape)?$this->escape($v):$v;
        
}
        
        
return $this;
    
 
Posted: 08 January 2008 01:29 PM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2006-03-23
3194 posts

Sorry, its fixed, but hasn’t made its way into the svn yet.

A few other things first… but very soon.

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design

 
Posted: 13 January 2008 05:37 AM   [ # 4 ]   [ Rating: 0 ]
Joined: 2006-07-14
4237 posts

I saw there was an addition in the SVN of the raw_where and raw_or_where methods will the set method have something similar?

 
Posted: 13 January 2008 09:52 AM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2006-03-23
3194 posts

Something similar yes.

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design

 
Posted: 13 January 2008 10:35 AM   [ # 6 ]   [ Rating: 0 ]
Joined: 2006-07-14
4237 posts

The reason why i asked the question is because i’m concerned with the naming of the methods. If similar functionality has different methods of calling it gets confusing.

So actually i was asking is if the the raw_ prefix is going to be used or setting the (third) argument to false?

 
Posted: 13 January 2008 11:29 AM   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2006-03-23
3194 posts

Good point, it does seem inconsistent, to have set() use a parameter for no escaping, but a whole new function for where().  That said, in order to achieve the same thing with a parameter in where we’d need to make it

$this->db->where('(field1 = var1 OR field2 = var2)'NULL FALSE); 

And really, that’s what raw_where() will be an alias for.  I just think its less confusing.

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design

 
Posted: 21 January 2008 09:00 PM   [ # 8 ]   [ Rating: 0 ]
Joined: 2002-03-12
17 posts

thanks xwero,

just tried the new set method with the new set query and it works well with incrementing/decrementing values.