Welcome Guest, Not a member yet? Register   Sign In
$this->load->database(); doesn't work in models
#1

[eluser]krankmelder[/eluser]
Hello everybody,

I know, that the problem with database connectivity has been discussed several times ago. But I spent half a day on checking every solution and going through the forum topics.
I am using Codeigniter 2.1.2 on a Windows machine with Apache and PHP 5 and MySQL (newest XAMPP)

Well problem is, that I cannot get the database connectivity for the models. In the controller however it works fine. Therfore a little example:

test.php (Controller)
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Test extends CI_Controller {

public function index()
{
  $this->output->enable_profiler(TRUE);

  log_message('debug', "XXXXXXXXXXXX - Load DB from CTLer now");
  $this->load->database();
  log_message('debug', "XXXXXXXXXXXX - Load DB from CTLer done?");
  
  
  log_message('debug', "XXYYYYYYAAA - Before Loginmodel-Load");
  $this->load->model('loginmodel');
  log_message('debug', "XXYYYYYYAAA - After Loginmodel-Load");
  
  
  $data['query'] = $this->loginmodel->checkDB();
}
}

loginmodel.php
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Loginmodel extends CI_Model {

function __construct()
{
  // Call the Model constructor
  parent::__construct();
  log_message('debug', "XXYYYYYYXXX - Constructor Loginmodel");  
  
  log_message('debug', "XXXXXXXXXXXX - Load Database now");
  $this->load->database(); // line 12
  log_message('debug', "XXXXXXXXXXXX - Load Database done?");
  
}

public function checkDB()
{  
  /*
   * Look up user in database
  */
  $query = $this->db->query('Select * from user');
  return $query->result();
}

}

Error message on website:
Quote:Fatal error: Call to a member function database() on a non-object in E:\xampp180\htdocs\pdbase_v1\application\models\loginmodel.php on line 12

Log:
Quote:DEBUG - 2012-09-05 13:57:03 --> Config Class Initialized
DEBUG - 2012-09-05 13:57:03 --> Hooks Class Initialized
DEBUG - 2012-09-05 13:57:03 --> Utf8 Class Initialized
DEBUG - 2012-09-05 13:57:03 --> UTF-8 Support Enabled
DEBUG - 2012-09-05 13:57:03 --> URI Class Initialized
DEBUG - 2012-09-05 13:57:03 --> Router Class Initialized
DEBUG - 2012-09-05 13:57:03 --> Output Class Initialized
DEBUG - 2012-09-05 13:57:03 --> Security Class Initialized
DEBUG - 2012-09-05 13:57:03 --> Input Class Initialized
DEBUG - 2012-09-05 13:57:03 --> XSS Filtering completed
DEBUG - 2012-09-05 13:57:03 --> Global POST and COOKIE data sanitized
DEBUG - 2012-09-05 13:57:03 --> Language Class Initialized
DEBUG - 2012-09-05 13:57:03 --> Loader Class Initialized
DEBUG - 2012-09-05 13:57:03 --> Helper loaded: url_helper
DEBUG - 2012-09-05 13:57:03 --> Helper loaded: form_helper
DEBUG - 2012-09-05 13:57:03 --> Form Validation Class Initialized
DEBUG - 2012-09-05 13:57:03 --> Controller Class Initialized
DEBUG - 2012-09-05 13:57:03 --> XXXXXXXXXXXX - Load DB from CTLer now
DEBUG - 2012-09-05 13:57:03 --> Database Driver Class Initialized
DEBUG - 2012-09-05 13:57:03 --> XXXXXXXXXXXX - Load DB from CTLer done?
DEBUG - 2012-09-05 13:57:03 --> XXYYYYYYAAA - Before Loginmodel-Load
DEBUG - 2012-09-05 13:57:03 --> Model Class Initialized
DEBUG - 2012-09-05 13:57:03 --> Model Class Initialized
DEBUG - 2012-09-05 13:57:03 --> XXYYYYYYXXX - Constructor Loginmodel
DEBUG - 2012-09-05 13:57:03 --> XXXXXXXXXXXX - Load Database now


Database is not autoloaded (but even if it is, there is no change)
Code:
$autoload['libraries'] = array(  'input', 'form_validation');
And it doesn't change anything, if I load the database in the CTLer or not.

So, where's the bug?

Thanks a lot!
#2

[eluser]TWP Marketing[/eluser]
Is there a reason you don't want to load it in our configuration: config/autoload.php
Code:
...
$autoload['libraries'] = array('database');
...
#3

[eluser]WanWizard[/eluser]
Just for fun, do a
Code:
var_dump($this->load);
before the load database. What does it say?
#4

[eluser]krankmelder[/eluser]
@TWP Marketing: Well I had autoload on before, but it doesn't make a difference. Also, I wanted to check, wether the $this->load->database() command works in the controler, which it does.

@WanWizard: Interesting, the var_dump in the Controler gives a lot of information, while the var_dump in the model returns NULL.
Code:
var_dump START in Controler
object(CI_Loader)#13 (12) {
  ["_ci_ob_level":protected]=>
  int(1)
  ["_ci_view_paths":protected]=>
  array(1) {
    ["application/views/"]=>
    bool(true)
  }
  ["_ci_library_paths":protected]=>
  array(2) {
    [0]=>
    string(12) "application/"
    [1]=>
    string(36) "E:/xampp180/htdocs/pdbase_v1/system/"
  }
  ["_ci_model_paths":protected]=>
  array(1) {
    [0]=>
    string(12) "application/"
  }
  ["_ci_helper_paths":protected]=>
  array(2) {
    [0]=>
    string(12) "application/"
    [1]=>
    string(36) "E:/xampp180/htdocs/pdbase_v1/system/"
  }
  ["_base_classes":protected]=>
  array(12) {
    ["benchmark"]=>
    string(9) "Benchmark"
    ["hooks"]=>
    string(5) "Hooks"
    ["config"]=>
    string(6) "Config"
    ["log"]=>
    string(3) "Log"
    ["utf8"]=>
    string(4) "Utf8"
    ["uri"]=>
    string(3) "URI"
    ["router"]=>
    string(6) "Router"
    ["output"]=>
    string(6) "Output"
    ["security"]=>
    string(8) "Security"
    ["input"]=>
    string(5) "Input"
    ["lang"]=>
    string(4) "Lang"
    ["loader"]=>
    string(6) "Loader"
  }
  ["_ci_cached_vars":protected]=>
  array(0) {
  }
  ["_ci_classes":protected]=>
  array(2) {
    ["session"]=>
    string(7) "session"
    ["form_validation"]=>
    string(15) "form_validation"
  }
  ["_ci_loaded_files":protected]=>
  array(2) {
    [0]=>
    string(57) "E:/xampp180/htdocs/pdbase_v1/system/libraries/Session.php"
    [1]=>
    string(65) "E:/xampp180/htdocs/pdbase_v1/system/libraries/Form_validation.php"
  }
  ["_ci_models":protected]=>
  array(0) {
  }
  ["_ci_helpers":protected]=>
  array(3) {
    ["url_helper"]=>
    bool(true)
    ["form_helper"]=>
    bool(true)
    ["string_helper"]=>
    bool(true)
  }
  ["_ci_varmap":protected]=>
  array(2) {
    ["unit_test"]=>
    string(4) "unit"
    ["user_agent"]=>
    string(5) "agent"
  }
}

var_dump END in Controler

var_dump START in Model
NULL

var_dump END in Model


Fatal error: Call to a member function database() on a non-object in E:\xampp180\htdocs\pdbase_v1\application\models\loginmodel.php on line 24

I reckon it's something with the CI instance? I had a similar problem I think back with CI 1.7 or something. But shouldn't it work like I did in CI 2.1.2?
#5

[eluser]Aken[/eluser]
For whatever reason, the __get() magic method in CI_Model is not being called properly. I can't really say where, but that's where your model would be going to find the load (and any other CI) property.
#6

[eluser]krankmelder[/eluser]
So where and how can I load this method?
Maybe somewhere in the constructor?
#7

[eluser]krankmelder[/eluser]
THERE WE GO....
Problem was I followed http://www.taggedzi.com/articles/display...eigniter-2 this tutorial to get auto-completion in Eclipse. I figured, that in the core-module Model.php a bug was inserted by me, because there the __get() gets loaded.

It looks like there was some problems with object or variable declarations.
The better autocompletion solution therefore seems to be this tutorial:
http://www.web-and-development.com/codei...ocomplete/

Man, that gave me a headache..
Thanks a lot to everybody!
- Cheers!
#8

[eluser]Aken[/eluser]
The __get() method is called when you try to access a class property that doesn't exist. Since you declared it in your CI_Model file, it exists, so __get() is never called, and the CI super object is never accessed.

The second tutorial will not work for models (at least I doubt it will). That's the problem with using magic methods and such.

Ideally, there should be configurable plugins for defining functions and such for IDE's. That's what I use with Coda on the Mac, and seems to make the most sense when it comes to updating them. Maybe Eclipse has something similar.
#9

[eluser]krankmelder[/eluser]
Well, after some testing, you are right, it doesn't work for my own models out of the box.
What one needs to do is:
1) create a fake_model.php with CI_Model instead of CI_Controler.
2) add every model one writes to the fake_controller.php and fake_model.php

fake_controller.php
Code:
<?php
class CI_Controller {
/**
  *
  * @var Mylogin - Does user login, logout, checks
  */
public $Mylogin;

/**
  *
  * @var Myget - Performs select-get actions on the database
  */
public $Myget;

/**
  *
  * @var CI_DB_active_record
  */
public $db; .............

Like this, you have auto-completion in Eclipse for Codeigniter 2.1.2. also for your self-written models.




Theme © iAndrew 2016 - Forum software by © MyBB