Sunday, August 31, 2014

how can use protected CI_DB_active_record->_compile_select() method as public get_compile_select()

1. copy system/core/Loader.php to application/core/Loader.php

2. copy system/database/DB.php to application/database/DB.php

3. open application/core/Loader.php, change method like this below


public function database($params = '', $return = FALSE, $active_record = NULL)
    {
        // Grab the super object
        $CI =& get_instance();
       
        // Do we even need to load the database class?
        if (class_exists('CI_DB') AND $return == FALSE AND $active_record == NULL AND isset($CI->db) AND is_object($CI->db))
        {
            return FALSE;
        }

        // BOROO edited this code [start]
        // _compile_select удамших функцийг ашиглахын тулд өөрчлөлт хийв
        //require_once(BASEPATH.'database/DB.php');
        require_once(APPPATH.'database/DB.php');
        // BOROO edited this code [start]

        if ($return === TRUE)
        {
            return DB($params, $active_record);
        }

        // Initialize the db variable.  Needed to prevent
        // reference errors with some configurations
        $CI->db = '';

        // Load the DB class
        $CI->db =& DB($params, $active_record);
    }


4. open application/database/DB.php, edit method like this below


require_once(BASEPATH . 'database/DB_driver.php');

    if (!isset($active_record) OR $active_record == TRUE) {
        require_once(BASEPATH . 'database/DB_active_rec.php');

        if (!class_exists('CI_DB')) {
            // BOROO edited this code [start]
            if(CI_VERSION == '2.1.4') {
                eval('class CI_DB extends CI_DB_active_record {
                    public function get_compile_select() {
                        return $this->_compile_select();
                    }
                }');
            } else {
                eval('class CI_DB extends CI_DB_active_record { }');
            }
            // BOROO edited this code [end]
        }
    } else {
        if (!class_exists('CI_DB')) {
            eval('class CI_DB extends CI_DB_driver { }');
        }
    }


5. now you can use get_compile_select()


in base_model extends CI_Model {

public function get_compile_select() {
        $this->{$this->db_group}->select('*');
        $this->{$this->db_group}->from($this->table);
        $this->{$this->db_group}->order_by("title", "asc");
        $this->{$this->db_group}->limit(100, 0);
        $subQuery = $this->{$this->db_group}->get_compile_select();
        return $subQuery;
    }
.................................
}

public category_model extends base_model {
..................................
}


in controller


echo "<hr>";
        echo $this->category_model->get_compile_select();
        echo "<hr>";
        exit;


result

SELECT * FROM (`category`) ORDER BY `title` asc LIMIT 100

//now u can get generate sql query without running or last_query()

No comments: