0

I have been using a library for subqueries to work - Subquery.php Ref : https://github.com/NTICompass/CodeIgniter-Subqueries

$this->db->select('test');
$this->db->select('test2');
$this->db->from('table');
$sub = $this->subquery->start_subquery('where_in');
$sub->select('IDs');
$sub->from('idTable');
$sub->where('date', '2011-07-10');
$this->subquery->end_subquery('id');

I think that this statement:

$sub = $this->subquery->start_subquery('where_in');

contains the error. When I execute this line, I get a blank page. The fn. start_subquery is:

function start_subquery($statement, $join_type = '', $join_on = 1){
        $db = $this->CI->load->database('', true); // after executing this statement, a blank page shows...
        $this->dbStack[] = $db;
        $this->statement[] = $statement;
        if(strtolower($statement) == 'join'){
            $this->join_type[] = $join_type;
            $this->join_on[] = $join_on;
        }
        return $db;
    }

FYI - In my database.php:

$active_group = 'default'
$active_record = TRUE;

And CI version is 2.1.0

  • Try in your controller: `$this->load->database('',true);` and check it! – uzsolt May 27 '12 at 07:35
  • Thanks for the reply but... No, its not working. :(. If that is the case, what should be replaced with the statement : $db = $this->CI->load->database('', true); in start_subquery function ? – Vishnu Prem May 27 '12 at 14:05
  • The `load->database()` should work. So please don't change! I think this subquery-library is ok. IMHO the problem is your database configuration in CI: http://codeigniter.com/user_guide/database/configuration.html Please check! – uzsolt May 27 '12 at 14:08
  • I have already checked the db config file and the doc.
    $active_group = 'default';
    $active_record = TRUE;

    $db['default']['hostname'] = 'localhost';
    $db['default']['username'] = '****';
    $db['default']['password'] = '***';
    $db['default']['database'] = '****';
    $db['default']['dbdriver'] = 'mysql';

    The rest of the config is default.
    – Vishnu Prem May 27 '12 at 14:36
  • Can you use any function of db? E.g. a simple `select` as can see http://codeigniter.com/user_guide/database/examples.html ? – uzsolt May 27 '12 at 15:35
  • Yes, of-course. I have been using $this->db->select, for E.g., in many of the model functions and those queries work perfect. – Vishnu Prem May 27 '12 at 16:31
  • It's strange. You call `load->database('',true)` and your queries doesn't work anymore? – uzsolt May 28 '12 at 07:08
  • I haven't called `load->database('',true)` in any of the controller functions for queries to execute. And yesterday, I have found a solution, and I don't know if it is the right way. Please correct me if I am wrong. As suggested in [this post](http://stackoverflow.com/questions/9232316/is-there-a-function-like-compile-select-or-get-compiled-select), I have added the function `get_compiled_select()` system > database > DB_active_rec.php, and the queries are working fine now. But I remember the guidelines of CI, that, the system folder should not be edited. Do you have a better solution ? – Vishnu Prem May 29 '12 at 05:41

3 Answers3

1

Hm, understand. As I see the subquery's source code in line 27, it wants to call _compile_select or get_compiled_select. If you can check in CI's DB_active_rec.php the _compile_select is protected so you can't access from Subquery (it isn't subclass of db).

Possible solution: _compile_select() should public or class Subquery should be extend of CI's db class. I think you should report this to author of Subquery.

Or you can extend the CI's db class :)

Sorry - I want to write it as a comment.

uzsolt
  • 5,832
  • 2
  • 20
  • 32
1

Thanks, I have changed the stmt. class Subquery to class Subquery extends CI_DB_active_record in Subquery.php and it is working fine. And yes, I will report this to the author. :)

  • `extends CI_DB_active_record` actually works? I thought CodeIgniter was removing the ability to extend DB classes. – gen_Eric Jul 09 '12 at 17:28
0

I wrote that library, so thanks for using it. The problem is (as @uzsolt has stated) is that _compile_select is protected. It used to not be in CodeIgniter 1.7.x (it was just a neat hidden method some devs found).

In the dev version of CodeIgniter on GitHub (https://github.com/EllisLab/CodeIgniter), there is a public get_compiled_select method. Seems this change hasn't been pushed to the stable build yet.

So, to get this library to work, you can try using the development version of CodeIgniter. I'm pretty sure they are going to remove the ability to extend CI_DB_active_record in the future (http://codeigniter.com/user_guide/general/creating_libraries.html).

Community
  • 1
  • 1
gen_Eric
  • 223,194
  • 41
  • 299
  • 337