Welcome Guest, Not a member yet? Register   Sign In
Put Transactions in the Controller or Model
#10

[eluser]treeface[/eluser]
Hello all. I've got an idea that extends from the logic of sketchynix. Instead of accessing the db class from the controller or putting a lot of extra logic in each model function, we have a third option: extending the model class. Consider this file sitting in your /application/libraries/MY_Model (though I believe you will need to load it in the autoload of your config):

Code:
<?php
class MY_Model extends Model
{
    //this is a variable that will let the two extension methods know
    //whether or not to start/override the trans_start and trans_complete
    var $maintain = false;
    
    function MY_Model()
    {
        parent::Model();
    }
    
    /**
     * This is our custom trans_start()
     *
     * @param bool    $maintain
     *
     * @return void
     */
    function trans_start($maintain = FALSE)
    {
        if ($this->maintain) {
            return TRUE;
        }
        
        $this->maintain = $maintain;
        $this->db->trans_start();
    }
    
    /**
     * This is our custom trans_complete()
     *
     * @param bool    $override
     *
     * @return void
     */
    function trans_complete($override = FALSE)
    {
        if ($override) {
            $this->maintain = FALSE;
        }
        
        if ($this->maintain) {
            return TRUE;
        }
        
        $this->maintain = $maintain;
        $this->db->trans_complete();
    }
}

Now we can maintain a trans_start as long as TRUE was passed to it and subsequently TRUE isn't passed to trans_complete. Of course, you'll have to refer to it as $this->trans_start() and $this->trans_complete() instead of $this->db... So here is what our model looks like now:

Code:
class Whatever_Model extends MY_Model
{
    function insertAndDelete()
    {
        //sets $maintain to true...now must be overridden to complete trans
        $this->trans_start(true);
        $this->insertSomething();
        $this->deleteSomething();
        //overrides $maintain and completes trans
        $this->trans_complete(true);
        
        return $this->db->trans_status() !== FALSE;
    }
    
    function insertSomething()
    {
        $this->trans_start();
        //perform query
        $this->trans_complete();
        
        return $this->db->trans_status() !== FALSE;
    }
    
    function deleteSomething()
    {
        $this->trans_start();
        //perform query
        $this->trans_complete();
        
        return $this->db->trans_status() !== FALSE;
    }
}

So since we've passed TRUE into the insertAndDelete trans_start, when it encounters $this->trans_start() and $this->trans_complete() in the insert and delete functions, it will skip it and perform the query. Thoughts?


Messages In This Thread
Put Transactions in the Controller or Model - by El Forum - 09-23-2010, 02:58 PM
Put Transactions in the Controller or Model - by El Forum - 09-23-2010, 03:19 PM
Put Transactions in the Controller or Model - by El Forum - 09-24-2010, 08:42 AM
Put Transactions in the Controller or Model - by El Forum - 09-24-2010, 09:25 AM
Put Transactions in the Controller or Model - by El Forum - 09-24-2010, 09:55 AM
Put Transactions in the Controller or Model - by El Forum - 09-24-2010, 10:20 AM
Put Transactions in the Controller or Model - by El Forum - 09-24-2010, 10:36 AM
Put Transactions in the Controller or Model - by El Forum - 09-24-2010, 10:43 AM
Put Transactions in the Controller or Model - by El Forum - 09-24-2010, 11:07 AM
Put Transactions in the Controller or Model - by El Forum - 09-24-2010, 12:31 PM
Put Transactions in the Controller or Model - by El Forum - 05-05-2011, 08:28 AM
Put Transactions in the Controller or Model - by El Forum - 06-09-2011, 04:44 AM
Put Transactions in the Controller or Model - by El Forum - 01-29-2012, 02:59 AM
Put Transactions in the Controller or Model - by El Forum - 12-15-2013, 04:29 AM



Theme © iAndrew 2016 - Forum software by © MyBB