Fork me on GitHub

Subqueries.
For CodeIgniter.

by NTICompass (aka Rocket Hazmat)

An active record subquery library for CodeIgniter.

This is a subquery library for CodeIgniter’s active record class. It lets you use the active record methods to create subqueries in your SQL.

Dependencies

PHP5
CodeIgniter 1.7.x - 2.0.2 (Works with 2.1.x, but some modifications are required)

Install

Put Subquery.php into /application/libraries,
then load it in your code using $this->load->library('subquery');.

I guess you can add 'subquery' to
$autoload['libraries'] (in /application/config/autoload.php), if you want.

CodeIgniter 2.1.x

This library doesn't work with CodeIgniter 2.1.x out of the box. It requires modifications to a file in /system to make it work.

You need to edit /system/database/DB_active_rec.php and modify the signature of _compile_select (should be line 1673). In the older version(s) of CodeIgniter, this function was not protected, so if you remove the protected keyword from the function, my library will work.

(There's probably a reason this function is protected.)

In the develop version of CodeIgniter (which works with this library just fine, by the way), there is a public function that you can use. You can "steal" the get_compiled_select function from the /system/database/DB_query_builder.php file (line 1283).

/**
 * Get SELECT query string
 *
 * Compiles a SELECT query string and returns the sql.
 *
 * @param    string    the table name to select from (optional)
 * @param    bool    TRUE: resets QB values; FALSE: leave QB vaules alone
 * @return    string
 */
public function get_compiled_select($table = '', $reset = TRUE)
{
    if ($table !== '')
    {
        $this->_track_aliases($table);
        $this->from($table);
    }

    $select = $this->_compile_select();

    if ($reset === TRUE)
    {
        $this->_reset_select();
    }

    return $select;
}
Put this function inside /system/database/DB_active_rec.php.

My library will check for the existance of either a _compile_select or get_compiled_select method. If none of these methods exist, the library will fail to load.

Authors

Rocket Hazmat(NTICompass@gmail.com)

Contact

Rocket Hazmat(NTICompass@gmail.com)

Download

You can download this project in either zip or tar formats.

You can also clone the project with Git by running:

$ git clone git://github.com/NTICompass/CodeIgniter-Subqueries