Fork me on GitHub

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.


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


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 !== '')

    $select = $this->_compile_select();

    if ($reset === TRUE)

    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.


Rocket Hazmat([email protected])


Rocket Hazmat([email protected])


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

You can also clone the project with Git by running:

$ git clone git://