CodeIgniter Forums
Active Record Return Result Object on Insert/Update? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Active Record Return Result Object on Insert/Update? (/showthread.php?tid=33465)



Active Record Return Result Object on Insert/Update? - El Forum - 08-26-2010

[eluser]kmanlove[/eluser]
It seems like there should be something like this on the forum, but I was unable to find it. Forgive me if you just end up forwarding me to a post.

I want insert and update to return a result object with what was just inserted, and it really seems like it should.

In ./system/database/DB_active_rec.php, I see
Code:
function insert($table = '', $set = NULL)
    {    
        if ( ! is_null($set))
        {
            $this->set($set);
        }
    
        if (count($this->ar_set) == 0)
        {
            if ($this->db_debug)
            {
                return $this->display_error('db_must_use_set');
            }
            return FALSE;
        }

        if ($table == '')
        {
            if ( ! isset($this->ar_from[0]))
            {
                if ($this->db_debug)
                {
                    return $this->display_error('db_must_set_table');
                }
                return FALSE;
            }
            
            $table = $this->ar_from[0];
        }

        $sql = $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
        
        $this->_reset_write();
        return $this->query($sql);        
    }

The important part being "return $this->query($sql);"

The header of query looks like this:
Code:
function query($sql, $binds = FALSE, $return_object = TRUE)

Return object is true... ?

Please help me if I'm misunderstanding, and if anyone has any implementation of this, that would be really great.

Thanks,
Keith


Active Record Return Result Object on Insert/Update? - El Forum - 08-26-2010

[eluser]WanWizard[/eluser]
In the CI active record class the 'write' type queries all return TRUE if they succeed.

There is no real way to get the last inserted record. Some databases have engine specific workarounds for that. For MySQL, you could retrieve it using the insert_id, but that requires a table with an auto_increment column, and a known name for that column.

For updates, it's a bit easier, since you have the table and the where clause ( they are both parameters of $this->db->update() ), so you could use the same to run a select with those parameters after an update.


Active Record Return Result Object on Insert/Update? - El Forum - 08-26-2010

[eluser]kmanlove[/eluser]
I was afraid of that... thanks for your help.