Welcome Guest, Not a member yet? Register   Sign In
What's the best way to determine if two tables were updated successfully using Active Record?
#7

[eluser]LeonardoGaiero[/eluser]
[quote author="mddd" date="1282914563"]On "select" type queries, the query object is returned. That's how you can chain it with result methods:
Code:
$this->db->get('table')->result();
On "write/update/delete" type queries, true or false is returned, indicating the succesful execution.

Note: this does NOT necessarily mean the database was altered. Consider this query:
Code:
update table set status='active' where id=10
As long as the table exists and it has a field 'active' this will always return true. Even if there is no record with id=10. So in this case you would have to check db->affected_rows() to be sure the database really changed.[/quote]

Thanks for your reply, that sounds like a reasonable solution which I didn't think about (please bear with me, I'm still familiarizing with CI's multitude of functions); however, I'm pondering an alternative solution since since I have to use transactions anyway, because the task requires me to manually pass the user information to the database for auditing purposes (long story). So at this point my situation is much more similar to this:

Code:
function insertData($table, $data) {
    $this->db->trans_start();
//    todo: insert userdata query
    $this->db->insert($table, $data); // this is the main query; the DB will trigger an error and refuse to continue if userdata is empty
    $this->db->trans_complete();
    return ($this->db->trans_status());
}

The way I understand it, if the return value is false that means the transaction failed and it's been rolled back; at that point I'm under the impression it would be safe to use that value as a reliable indicator, and halt all subsequent instructions safely. Would there be any further issues in handling exceptions like this? My apologies for the mild OT.


Messages In This Thread
What's the best way to determine if two tables were updated successfully using Active Record? - by El Forum - 08-27-2010, 03:28 AM



Theme © iAndrew 2016 - Forum software by © MyBB