Welcome Guest, Not a member yet? Register   Sign In
Incrementing a field value using $this->db->update()
#1

[eluser]TheFuzzy0ne[/eluser]
Hi everyone.

Does anyone know if it's possible to increment a field value using $this->db->update()? The user guide shows how to use $this->db->set() with the optional third parameter, but this doesn't fit in with what I need, as I need to update several other fields at the same time.

I'd appreciate any suggestions from anyone who's been in a similar position.

Thanks in advance.
#2

[eluser]Dam1an[/eluser]
Is it not possible to update multiple fields with set?
The user guide shows it using set multiple times for an insert, I'd assume the update would be the same
#3

[eluser]Dam1an[/eluser]
Edit: :grrr: Why does it not post and then double post :-S
#4

[eluser]TheFuzzy0ne[/eluser]
OK. This was my update method in my model:

Code:
function _update($forum)
{
    $this->db->where('id', $forum['id']);
    unset($forum['id']);
        
    return $this->db->update($this->table, $forum);
}

Fairly primitive really.

Here's the same method, only pimped:
Code:
function _update($forum)
{
    $this->db->where('id', $forum['id']);
    unset($forum['id']);

    foreach ($forum as $key => $val)
    {
        if (preg_match('/^([a-z0-9_]+)\s?\+\s?\d+$/i', $val, $matches))
        {
            if (isset($this->fields[$matches[1]]))
            {
                $this->db->set($key, $val, FALSE);
                unset($forum[$key]);
            }
        }
    }

    return $this->db->update($this->table, $forum);
}

All of my models contain a list of fields, so I loop through the array, looking for a pattern that matches /^([a-z0-9_]+)\s?\+\s?\d+$/i, then I check the field name, to ensure it's actually a field name, and then I do a set manually, and remove it from the array. Ideally, I don't want any other values to be inserted without first being escaped. Any further suggestions for improvement are welcomed.

Perhaps I should just call set on each row, since I'm looping through the array anyway?

Thanks for your comments, Dam1an. Stop double-posting - it's naughty. Tongue




Theme © iAndrew 2016 - Forum software by © MyBB