using libraries in models - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: using libraries in models (/showthread.php?tid=8740) Pages:
1
2
|
using libraries in models - El Forum - 05-29-2008 [eluser]crumpet[/eluser] Well you can't use libraries in models apparently ... and helpers don't seem to work either... but I want several of my models to use the same functions which I want located in a central place is there anything like a library which i dont know about or any way to get around the models not having libraries issue using libraries in models - El Forum - 05-29-2008 [eluser]gtech[/eluser] you can load a model in a model, so you can have a common model if thats what you are after. You can achieve this by loading the 'common' model in the every models constructor that uses the common model, this means it being available in every function. Or you can get the CI instance in a function and load the model through the CI instance object (there are examples of this in the forum). you may be also able to extend the common model in the class definition (I must admit I have not tried this approach though). using libraries in models - El Forum - 05-29-2008 [eluser]onejaguar[/eluser] You shouldn't have any problems loading and using helpers or libraries from your model. E.g. Code: $this->load->library('image_lib'); Should all work just fine inside one of your model's methods. using libraries in models - El Forum - 05-29-2008 [eluser]crumpet[/eluser] doesn't seem to be working for me i do $this->load->library('lib'); $this->lib->libmethod(); and i get error which says somethign like $lib doesn't exist can't use libmethod() of a non object... same for helpers so far.. i'll try the model approach using libraries in models - El Forum - 05-29-2008 [eluser]Seppo[/eluser] Can you specify you PHP version, crumpet? Models should be able to load libraries... I'll test that again now, but more data will help =) -Edit- Working fine on all php versions I have using libraries in models - El Forum - 05-29-2008 [eluser]gtech[/eluser] yes I have tested loading a library in a model and it works, not a good example of echoing content in a model! but just to show it works. controller (application/controllers/temp.php): Code: <?php Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); Code: <?php to add to what the others have said have you actually loaded the library in the controller to see if it works? using libraries in models - El Forum - 05-29-2008 [eluser]Seppo[/eluser] Oh oh I got it! =) Are you autoloading the model and loading the library in your constructor? If that's so that's the problem... my code example The autoload config Code: $autoload['model'] = array('testmodel'); The testmodel model Code: <?php using libraries in models - El Forum - 05-30-2008 [eluser]crumpet[/eluser] hmm i feel really stupid but it didn't seem to work .. i've rewritten the code since then to not use libraries in the model but I tested it pretty throughly... i wasn't autoloading any of hte libraries/models so i don't think thats a problem. And though originally I was loading the library in the constructor of the model I tried loading it in the same function as the library function is called. And yes the library works perfectly in a controller and i copy pasted the code for loading the library there into the model. So I just tested it using bare bones functions and that seemed to work so it must have been an error in my code elsewhere...really strange though because i'm sure i tested it this way before thanks everyone for your help I will post again if I figure out what the problem was exactly. using libraries in models - El Forum - 05-30-2008 [eluser]gtech[/eluser] was a good call by Seppo, maybe you were getting some strange php error, worth upping the log_threshold number it the config file to see if anything gets logged next time. using libraries in models - El Forum - 06-02-2008 [eluser]David B.[/eluser] Hello, I think I have kind of a similar problem. I'm not autoloading the model but I'm loading the the library in the constructor of my model. And I can not access the methods of the library in the constructor - only afterwards. Please, can someone describe what is the problem with this? And would the preferred solution for that be an additional init-method in the model that then should be called in the controller after loading the model? E.g.: Controller: Code: class Mycontroller extends Controller Model: Code: class Mymodel extends Model Thanks for your thoughts! David |