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

[eluser]xcristi[/eluser]
Hello,

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?

Thanks.
Cristian
#2

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

Code:
$CI =& get_instance();
$CI->load->database();
#3

[eluser]xcristi[/eluser]
Thanks a lot man.

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

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

Code:
$CI =& get_instance();
$CI->load->database();
[/quote]

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?
#5

[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->....
#6

[eluser]sangharsha[/eluser]
Thanks for answer.
Lemme re-phrase the situation:

I have regular classes in Models which use:

Code:
$this->db->query($some_sql);
...

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

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

Code:
$this->db->query($some_sql3);
...
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.
#7

[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