EllisLab/CodeIgniter — pull request #2665 by waitingsong (2013-09-30 07:06:04+00:00)
Title: Add RETURNING statement support within INSERT/UPDATE/DELETE query via $this->db->returning() for pgsql

Add RETURNING statement support within INSERT/UPDATE/DELETE query via $this->db->returning() for pgsql

$this->db->returning($cols, $escape=NULL), the $cols can be string or array. eg.
return column(s) 'id, name' or array('id', 'name'), or '*' to returning the entire record row.
return expression '1+1' or 'price * 2'
the returning result can be retrived through $query->result() or $query->row()

demo:
db->returning('project_id,project_status')->where('project_id', 8)->or_where('project_id', 9)->update('project', array('project_status' => 6));
if ($res->num_rows())
{
foreach ($res->result() as $row)
{
// do something
}
}

--------------------------------------------

$res = $this->db->returning('project_status')->where('project_id', 1)->delete('project');
if ($res->num_rows())
{
$status = $res->row()->project_status; // at most one row
}

---------------------------------------------

$res = $this->db->returning('project_amount * 1.5 AS amount_new', FALSE)->where('project_amount >', 100)->update('project', ...);
if ($res->num_rows())
{
foreach ($res->result() as $row)
{
$amount = $row->amount_new;
// do other
}
}

?>