Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter/PHP Active Record won't increment an integer

Here's my query, in CodeIgniter's Active Record:

function calculate_invites($userid)
{
    $this->db->where('id', $userid)
               ->update('users', array('invites' => 'invites-1', 'sentinvites' => 'sentinvites+1'), FALSE);
}

The fields invites and sentinvites are both integers but are set to 0 after the function is run. This makes me presume that CodeIgniter is passing invites-1 and sentinvites+1 as strings, but I thought appending FALSE to the end stopped it doing that?

Thanks!

Jack

like image 628
Jack Avatar asked Sep 05 '10 14:09

Jack


People also ask

What is CodeIgniter active record class?

Active Record Class CodeIgniter uses a modified version of the Active Record Database Pattern. This pattern allows information to be retrieved, inserted, and updated in your database with minimal scripting. In some cases only one or two lines of code are necessary to perform a database action.

Does CodeIgniter ignore the second parameter of an array key?

Apparently, if $key is an array, codeigniter will simply ignore the second parameter $value, but the third parameter $escape will still work throughout the iteration of $key, so in this situation, the following codes work (using the chain method): However, this will unescape all the data, so you can break your data into two parts:

What is the CodeIgniter database pattern?

This pattern allows information to be retrieved, inserted, and updated in your database with minimal scripting. In some cases only one or two lines of code are necessary to perform a database action. CodeIgniter does not require that each database table be its own class file. It instead provides a more simplified interface.

How to add GMT time offset in CodeIgniter?

you can load the date helper and use the codeigniter interal now () if you want to reference the users GMT time offset If you don't use the GTM master settings and let your users set their own offsets there is no advantage over using php's time () function.


1 Answers

This doesn't work with update, only with set.

This should work:

$this->db->where('id', $userid);
$this->db->set('invites', 'invites-1', FALSE);
$this->db->set('sentinvites', 'sentinvites+1', FALSE);
$this->db->update('users');

This may work too (the user guide is a bit unclear):

$this->db->where('id', $userid);
$this->db->set(array('invites' => 'invites-1', 'sentinvites' => 'sentinvites+1'), FALSE);
$this->db->update('users');
like image 65
Mischa Avatar answered Sep 22 '22 07:09

Mischa