• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is this a bug or just my bad practice?

#1
Question 
I think that I found a bug or just did some mess while using $this->db->select() method on my code. I'm loading all models by autoload.php, and I gave different names to all of them. But when I started to call them inside the controller, something curious happened: a portion of $this->db->select() that I used in one method of a model was interfering in the behavior of other method in another model.

Considering that the models are all loaded by autoload.php, I called its methods in my controller like this:
Code:
$arr1 = $this->model1->method1();
$arr2 = $this->model2->method2();

Then, an error message was returned:
Code:
A PHP Error was encountered
Severity: Warning
Message: pg_query(): Query failed: ERRO: column "field1" does not exist LINE 1: SELECT "field1" ^
Filename: postgre/postgre_driver.php
Line Number: 242

Here are the samples of the two methods from my models:

model1 method:
Code:
    function method1($id){
        $array = [];
        $this->db->select('field1');
        $data = ['id' => $this->db->escape_str($id)];
        $query = $this->db->get_where(self::TABLE, $data);
        foreach ($query->result_array() as $var){
            $array[] = $var['field1'];
        }
        return $array;
    }

model2 method:
Code:
    function method2($id){
        $array = [];
        $this->db->select('field2, field4');
        $data = ['id' => $this->db->escape_str($id)];
        $query = $this->db->get_where(self::TABLE, $data);
        foreach ($query->result_array() as $var){
            $array[] = $var['field2'].$var['field4'];
        }
        return $array;
    }

I've noted that removing $this->db->select('field1'), makes the things work fine.

So, it is a bug or just my bad? Huh
Reply

#2
CI caches the database queries there is a setting for save_queries in the database.php config file

not sure if it will help but give it a try.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
Keep in mind that the entries in the autoload config are causing CI to load the files and instantiate objects for the models, libraries, etc. for every single request handled by CI, so you really only want them in there if you are absolutely going to use them for everything you do.

save_queries is a debug setting enabled by default (and not normally included in the config file), the setting you want to look for is cache_on.

Otherwise, the select statement is usually reset when you call get_where(), so this shouldn't happen unless $this->db->select() is called again after the call to $this->db->get_where().

This may be unrelated, but depending on your configuration, you could potentially be double-escaping your $id value, since get_where() usually escapes the data passed to its second argument.
Reply

#4
(07-07-2016, 10:34 AM)InsiteFX Wrote: CI caches the database queries there is a setting for save_queries in the database.php config file

not sure if it will help but give it a try.

Thank you for the attention. It was just a little doubt, not a really a problem. Wink
Reply

#5
(07-07-2016, 10:34 AM)InsiteFX Wrote: CI caches the database queries there is a setting for save_queries in the database.php config file

not sure if it will help but give it a try.

This is completely irrelevant. It can't possibly help.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.