Welcome Guest, Not a member yet? Register   Sign In
ActiveRecord Debugging Library
#1

[eluser]Phil Sturgeon[/eluser]
I recently wrote an article on how to debug your ActiveRecord queries but I think it could be even easier.

Here is a rough protoype for a VERY simple debugging library that will help output ANY ActiveRecord query.

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package        CodeIgniter
* @author        ExpressionEngine Dev Team
* @copyright    Copyright (c) 2008, EllisLab, Inc.
* @license        http://ellislab.com/codeigniter/user-guide/license.html
* @link        http://codeigniter.com
* @since        Version 1.0
* @filesource
*/

// ------------------------------------------------------------------------

/**
* DB Debug Class
*
* Helps you debug your ActiveRecord queries over mail, log or screen
*
* @package        CodeIgniter
* @subpackage    Libraries
* @category    Database
* @author        Phil Sturgeon < [email protected] >
* @link        
*/

class DB_debug extends CI_DB_active_record {
    
    var $_email; // Used to send emails from the server
    var $_mode;
    var $_run_query = FALSE;
    
    function mode($mode, $email = NULL)
    {
        $this->_mode = $mode;
        $this->_email = $email;
        
        return $this;
    }
    
    function run()
    {
        $this->_run_query = TRUE;
        
        return $this;
    }
    
    function query($sql)
    {
        log_message('debug', 'DB Deug class produced: '.htmlentities($sql) );
        
        switch($this->_mode)
        {
            case 'email':
                $this->load->library('email');

                $this->email->to($this->_email);
                
                $this->email->subject('SQL Debug: '.time());
                $this->email->message('SQL query: '.$sql);
                
                $this->email->send();
            break;
            
            case 'echo':
                echo $sql;
            break;
            
            case 'exit':
                exit($sql);
            break;
        }
        
    }
}

/* End of file DB_active_rec.php */
/* Location: ./system/application/libraries/DB_debug.php */

Theoretically (untested) you could run any of your normal commands such as get, getwhere, update, select, etc through $this->db_debug->get(); then it would work fine. For example:

Code:
$this->db->set('something', $variable);
$this->db->where('whatever', 'stuff');

// Just output to screen
$this->db_debug->mode('echo');
$this->db_debug->update('table');

// Stop page from going any further
$this->db_debug->mode('exit');
$this->db_debug->update('table');

// E-mail you the query
$this->db_debug->mode('email', '[email protected]');
$this->db_debug->update('table');

All of these will be logged in the CodeIgniter logs too.

Totally untested, just looking for feedback.


Messages In This Thread
ActiveRecord Debugging Library - by El Forum - 06-01-2009, 05:48 AM
ActiveRecord Debugging Library - by El Forum - 06-01-2009, 05:53 AM
ActiveRecord Debugging Library - by El Forum - 06-01-2009, 07:07 AM
ActiveRecord Debugging Library - by El Forum - 06-01-2009, 07:18 AM
ActiveRecord Debugging Library - by El Forum - 06-01-2009, 08:02 AM
ActiveRecord Debugging Library - by El Forum - 06-01-2009, 08:41 AM
ActiveRecord Debugging Library - by El Forum - 06-01-2009, 08:48 AM
ActiveRecord Debugging Library - by El Forum - 06-01-2009, 12:41 PM
ActiveRecord Debugging Library - by El Forum - 06-01-2009, 12:42 PM
ActiveRecord Debugging Library - by El Forum - 06-01-2009, 12:45 PM
ActiveRecord Debugging Library - by El Forum - 06-01-2009, 03:02 PM
ActiveRecord Debugging Library - by El Forum - 06-05-2009, 07:56 PM
ActiveRecord Debugging Library - by El Forum - 06-06-2009, 03:37 AM



Theme © iAndrew 2016 - Forum software by © MyBB