$this->load->database(); doesn't work in models |
[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'); loginmodel.php Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 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 Database is not autoloaded (but even if it is, there is no change) Code: $autoload['libraries'] = array( 'input', 'form_validation'); So, where's the bug? Thanks a lot!
[eluser]TWP Marketing[/eluser]
Is there a reason you don't want to load it in our configuration: config/autoload.php Code: ...
[eluser]WanWizard[/eluser]
Just for fun, do a Code: var_dump($this->load);
[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 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?
[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.
[eluser]krankmelder[/eluser]
So where and how can I load this method? Maybe somewhere in the constructor?
[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!
[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.
[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 Like this, you have auto-completion in Eclipse for Codeigniter 2.1.2. also for your self-written models. |
Welcome Guest, Not a member yet? Register Sign In |