Welcome Guest, Not a member yet? Register   Sign In
In a plugin I don't have access to database?


My problem is giving me headaches:
I have a plugin placed in application/plugins, called menu_pi.php. From this plugin I need to access my postgresql database. From a model I have no problem. But inside the plugin the code

$this->db->get('meniu'); doesn't work. So I think so, because my site get a blank screen.

My question: plugins cannot access database?


[eluser]Glen Swinfield[/eluser]
You need to get an instance of CI first then load the database:

$CI =& get_instance();

Thanks a lot man.

That was the way. And then I access as: $CI->db->get('meniu');

[quote author="Glen Swinfield" date="1187711073"]You need to get an instance of CI first then load the database:

$CI =& get_instance();

I had the similar problem where the DB object gets lost, and calling the get_instance() I could make database queries.
But this triggered another problem: the original instance of the CI object gets lost, and the subsequent calls to $this->db->query() doesn't work anymore.
And again I have to call the get_instance() to make the queries. Is there any way to restore the previous object?

[eluser]Pascal Kriete[/eluser]
I can't say I understand what you're saying. The $CI object will only be available in the scope you created it in.

Also, you will need to use $CI->db->query instead $this->....

Thanks for answer.
Lemme re-phrase the situation:

I have regular classes in Models which use:


And I also have a plugin, which needs database access, hence there I use
$this->NewObj =& get_instance();

But now when the control gets back to normal classes, I am unable to use

The database query simply doesn't run.

So is there any way I can again access the main CI object?

Was this understandable?
I'll further clarify if needed.
Thanks again.

[eluser]Pascal Kriete[/eluser]
Ah ok, quite understandable now Smile.

I don't know what is doing that though. By using =& you're passing a reference, so you're not actually changing anything. Can you try to reduce the problem to as few lines of code as possible and post it. I've never seen this behavior.

Theme © iAndrew 2016 - Forum software by © MyBB