Welcome Guest, Not a member yet? Register   Sign In
$CI->db object pointing to the wrong database
#1

[eluser]D_Williams[/eluser]
Specifically I'm attempting to use the Google Maps API library as found on the wiki at http://codeigniter.com/wiki/Google_Maps_API_V3_Library/ to map a bunch of addresses from the database on a map.

It, like most other libraries, has to use get_instance() or whatever to get its CI object. The only database querying it does that I can see is for the geocode caching. Now, in my project, I actually use two databases. One is my default database that is always loaded, and another one I connect to in certain models. In my models that use this database I have a private $CMAX variable and in the constructor I connect to it as follows:

Code:
$this->CMAX = $this->load->database('cmax', TRUE);

From then on I can use $this->CMAX to interact with that database while still using $this->db to interact with my primary database. However, I've run into a problem. The geocode cache table is in my primary database which doesn't seem like a problem. In my script, I call a model that uses the secondary database to retrieve a bunch of addresses and return the list to my controller at which point I loop through and add them all as markers. This logic seems sound in my head but when I try to execute it I get an error saying that the "geocode_cache" table does not exist in my secondary database, which is true, it exists in my primary database which is what I expected it to be using.

The line where the GMap library queries the database seems to be as following
Code:
$_res = $this->CI->db->select('lat,lng')->from($this->_db_cache_table)->where('query', strtolower($address))->get();

Now, it uses $this->CI->db which should reference my primary default database, correct? for some reason it's apparently hitting my second database. Is this a bug or something? If I remove the bits of code that query the secondary database and just hardcode in a sample address it works fine.

Here's my full controller function for this:
Code:
$this->load->library('GMap');
        $this->load->model('account_model');
        $this->gmap->GoogleMapAPI();
        $this->gmap->setMapType('hybrid');
        
        $addrs = $this->account_model->get_fields('fullname, filenumber, clientname, address, city, state, zip');
        
        foreach($addrs as $a)
        {
            $fullAddress = $a['address'] . ', ' . $a['city'] . ' ' . $a['state'] . ' ' . $a['zip'];
            $this->gmap->addMarkerByAddress($fullAddress, "#{$a['filenumber']} - {$a['fullname']}", "Client of {$a['clientname']}");
        }

        $data['headerjs'] = $this->gmap->getHeaderJS();
        $data['headermap'] = $this->gmap->getMapJS();
        $data['onload'] = $this->gmap->printOnLoad();
        $data['map'] = $this->gmap->printMap();
        $data['sidebar'] = $this->gmap->printSidebar();

        $this->load->view('geo', $data);

Also, as an unrelated question, are the addresses I add as markers ever sent to Google's servers? This is an in-house web application and the addresses are technically private information and thus need to stay between my webserver and the local workstations viewing it.
#2

[eluser]D_Williams[/eluser]
Bump. Still don't know whether I'm doing something wrong or if this is a bug...
#3

[eluser]WanWizard[/eluser]
I can't reproduce it.

I've done a test install of a standard CI 1.7.2.

In config/database.php I've created two entries, 'system', which is loaded by default, and 'test'. I have the database library autoloaded. I then created a library testlib. In the index method of the welcome controller, I added
Code:
$this->load->library('Testlib');
$this->testlib->test();

The library itself:
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Testlib
{
    function test()
    {
        $CI =& get_instance();

        // default database instance
        var_dump($CI->db);
        $query = $CI->db->get('users');
        var_dump($query->result());

        // our local database instance
        $this->CMAX = $CI->load->database('test', TRUE);
        var_dump($this->CMAX);
        $query = $this->CMAX->get('users');
        var_dump($query->result());
    }
}

This works as advertised. The var_dumps() of the database objects show two different configurations, and both queries return different results.

If I change $this->CMAX to $CI->CMAX, the profiler picks the object up, and shows me the queries for both databases.




Theme © iAndrew 2016 - Forum software by © MyBB