Welcome Guest, Not a member yet? Register   Sign In
Newbie: Database Access from Model
#11

[eluser]cideveloper[/eluser]
[quote author="InsiteFX" date="1297340718"]If there is no data that is returned result will be an empty array!

InsiteFX[/quote]

Thats not the problem, InsiteFX. He is getting errors way before eevn trying to get the results like
Quote:Fatal error: Call to a member function limit() on a non-object

There is an issue with the model not being able to access the auto loaded db library.
I wonder if mi6crazyheart solution was able to help. Maybe loading the database in the model might help. The problem with this is that even if it works, there is still something wrong with his CI install
#12

[eluser]Andy MC[/eluser]
Thanks for all the replies and I have a solution.

Jacktoolson suggested adding '$ci =& get_instance();' within the model which I did and amended all references within the function from $this to $ci. This resulted in an error saying that variable $ci was undefined.

When I move the statement to the first line within the function everything worked as expected.

I'm now going to read up on get_instance() but can someone confirm my solution is correct. I am slightly confused as to why it doesn't work when the statement is placed within the constructor.

Cheers,

Andy.
#13

[eluser]Andy MC[/eluser]
Ok, following up in the CI documentation, the section 'Utilizing CodeIgniter Resources within Your Library' within 'Creating Libraries' says '$this' should be available within controllers, models and views, as I expect it should.

This is where I start to go red with embarressment, says I have tried this in 3 different subdirectories within wwwroot aptly named test1, test2 and test3, and I've now created a 4th. Hey presto, it worked as originally posted. Not convinced I created a 5th and it still works. Unfortunately, I've removed the directories so can't do a diff.

I'd like to be able to offer an explanation, and should I ever find one I will update this post, but at the moment I'm stumped.

Thanks very much for all your help and I apologies if I've wasted too much of anyones time.

Andy.
#14

[eluser]Unknown[/eluser]
I had the same issue, and a really dumb yet well hidden mistake was done.
I was originally not using the CI framework yet still using an mvc pattern. So a lot of my files used were copied over directly making a few tweaks to make them 'visible' to CI.
Anyway I had a few instance variables in my model, one of which was $db, for my old database connection.

needless to say $this->db->get was accessing the local instance variable $db through '$this->db' and not the global $db.
Not sure if that was the issue in this case. But be sure to check that out.

Im sure Andy is a pro by now at CI, but for the rest of you like me who stumbled across this error: check what you name your database connection isn't conflicting inside that class!




Theme © iAndrew 2016 - Forum software by © MyBB