• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
load->database() limitation or bug?

#1
[eluser]Unknown[/eluser]
I'm working on a simple Vbulletin integration with a portal, but I found a strange issue:

I have two functions in Member model, that retrieves user info. The first one retrieve info from VBulletin database and the second one retrieves Extra info from antoher db.

But the second function tries to retrieve info from the database of the first function, even with wrong prefix.

Here's the code:

Member Controller:
Code:
class Users extends Controller {
        public function ___construct()
        {
           .....
        }

        function index()....

    function username($username)
    {
        .... Assigning HTML title, CSS, JS for this controller to pass later to view
//USER DATA
        $this->load->model('membro');
        $info['vbinfo'] = $this->membro->getUserInfo($username);
        if ($info['vbinfo']->userid > 0) {
            //EXTRA DATA
            $info['info'] = $this->membro->getUserBasicInfo($info['vbinfo']->userid);
        }
        
        //MODULOS
        $data['modulos'] = $this->load->view('membros/membroModule', $info, TRUE);

The Member Model:
Code:
class Membro extends Model {
    
    private $results;
    
    public function __construct()
    {
        parent::Model();
        $this->results = new stdClass();
        $this->results->username = 'guest';
        $this->results->userid = -1;
        $this->results->usergroup = 0;
        
    }
    
    public function getUserInfo ($username)
    {
        $this->load->database('vb');
        $this->db->from('user')->where('username', $username)->limit(1);
        $query = $this->db->get();
        if ($query->num_rows > 0) $this->results = $query->row();
        $this->db->close();
        
        return $this->results;
    }
    
    public function getUserBasicInfo ($userid)
    {
        $results = new stdClass();
        $results->paisOrigem = ('00');
        $results->estadoOrigem = ('00');

        $this->load->database('default');
        $this->db->from('user_info')->where('userid', $userid)->limit(1);
        $query = $this->db->get();
        if ($query->num_rows > 0) $results = $query->row();
        
        return $results;
    }
    
}

The DB Config (database.php):
Code:
$active_group = "default";
$active_record = TRUE;

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "";
$db['default']['password'] = "";
$db['default']['database'] = "portal";
$db['default']['dbdriver'] = "mysqli";
$db['default']['dbprefix'] = "portal_";
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_unicode_ci";
//VBulletin Database
$db['vb']['hostname'] = "localhost";
$db['vb']['username'] = "";
$db['vb']['password'] = "";
$db['vb']['database'] = "vbulletin";
$db['vb']['dbdriver'] = "mysqli";
$db['vb']['dbprefix'] = "forum_";
$db['vb']['pconnect'] = FALSE;
$db['vb']['db_debug'] = TRUE;
$db['vb']['cache_on'] = FALSE;
$db['vb']['cachedir'] = "";
$db['vb']['char_set'] = "utf8";
$db['vb']['dbcollat'] = "utf8_unicode_ci";

When i do this:
Code:
$this->load->model('membro');
        $info['vbinfo'] = $this->membro->getUserInfo($username);
And pass to view, i got the correct info. Only from VBulletin.

OR when i do this:
Code:
$this->load->model('membro');
        //$info['vbinfo'] = $this->membro->getUserInfo($username);
$testuser = 50;        
if ($testuser > 0) {
            //EXTRA DATA
            $info['info'] = $this->membro->getUserBasicInfo($testuser);
        }
I got the correct extra info for the desired user.

But when i tried the first controller code, loading vbulletin info first, and if exists user, load extra info, i got no luck. I got this error:
Code:
A Database Error Occurred

Error Number: 0

SELECT * FROM (`forum_user_info`) WHERE `userid` = '50' LIMIT 1

It clear that CodeIgniter is searching in the wrong database config. The correct should be ('portal_user_info'), that leads to default DB config, and not the VB config.

Any tips, or this is really a bug?

For me it looks a limitation bug, because i used $this->load->database() twice in the same model, for different DBs.

#2
[eluser]Unknown[/eluser]
Hi again, I debugged more things:

It appears that vbdevtools hook for CI is interfering with CI's load->database, leading to connecting only with vb database, and "blocking" the access to another DB.
URL: http://sourceforge.net/projects/vbdevtools/

But not appears to be a *only* vbdevtools bug, because i created another CI app, only to test the above reported bug, without any hook. The same error occurs.

This time i created 2 models, one loading 'vbulletin' database and the second loding 'portal' database, in the same controller:
- Controller "member"
1. ->Load first model->Load first DB; (Infos from VBulletin)
2. ->Load second model->Load another DB; (Info from another DB)
3. ->Pass info to my "Bug-Hunt" View

The first loaded model returns the info from vb database wthout error. The second loaded model tries to retrieve info again from vB DB, and not from 'portal' DB.
If I comment the loading of first model, the second works, loading the data from the correct database and prefixing the SELECT query correctly.

Very very weird... I'll try Kohana PHP with similar code, to see if is something in my code and confirm more things.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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