Welcome Guest, Not a member yet? Register   Sign In
[SOLVES] Can you have multiple Active Record objects?
#1

[eluser]Unknown[/eluser]
I'm looking to record changes to my database by calling the $this->recordTx() method before calling $this->db->update(). Is there a way to create a second Active Record object so that I can do an insert without disrupting the original Active Record object that hasn't been completed with update() yet? The reason i'm doing this is that I need to select the previous values of each row to be affected by the update so that I can store them in case I want to roll back.

Code:
<?php

/**
* Extends CI Model to add some extra methods
*
* @author Craig Boileau
*/
class MY_Model extends CI_Model{
    
    function __construct(){
        parent::__construct();
    }
    
    function recordTx($table, $data){
        $where=$this->db->ar_where;
        $results=$this->db->where($where[0])->get($table);
        foreach ($results->result() as $row)
        {
            echo "Updating Claim Record #".$row->claim_id.":<br />";
            foreach($data as $key=>$val){
                if($row->$key != $val){
                    echo "<li>Updating Field $key";
                    echo "<li>Old Value: ".$row->$key."</li>";
                    echo "<li>New Value: ".$val."</li>";
                    echo "</li>";
                    /* Insert Old/New values to a table called transactions
                       Second Active Record Object would go here if possible
                    */
                }
            }
        }
    }

}

?&gt;


Here is an example of how I would like to make the calls to this function.
Code:
$this->db->where("row_id", $data["row_id"]);
                $this->recordTx("tablename", $update_data);
                
  $result = $this->db->update("tablename", $update_data);

EDIT: I've resolved this by making a transactions model, and loading that model within my other models.




Theme © iAndrew 2016 - Forum software by © MyBB