![]() |
Independent database - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Independent database (/showthread.php?tid=7329) |
Independent database - El Forum - 04-03-2008 [eluser]Diego Pessoa[/eluser] Hi, I'm trying to use the CI follow the OOP paradigm, but, a thing is incommode me, the database is attached in the CI Model class, specifically, to use the database: Code: $this->db->get('table') Follow the OOP rules in a class I need of static methods, example: Code: class User extends Model{ How I can to use the database in static methods with CI? Thankx, Diego Pessoa Independent database - El Forum - 04-03-2008 [eluser]wiredesignz[/eluser] Unfortunately the database object (db) is attached to the Controller with a reference available to your Model, so I can't see how static mehod calls are possible. Independent database - El Forum - 04-03-2008 [eluser]Diego Pessoa[/eluser] In my opinion the database access should be independent, I don't like of this attach. Independent: Code: $db = new DB(parameters); Exists a way to make this with CI? Independent database - El Forum - 04-03-2008 [eluser]wiredesignz[/eluser] Yes, you can load the database object into your own variable. Code: $db = $this->load->database($params, TRUE); Independent database - El Forum - 04-03-2008 [eluser]Seppo[/eluser] Or you can use... Code: $CI =& get_instance(); Independent database - El Forum - 04-03-2008 [eluser]wiredesignz[/eluser] $CI is the controller, so it is no different to using $this. Independent database - El Forum - 04-03-2008 [eluser]Seppo[/eluser] I meant inside User::getUserById static method. Independent database - El Forum - 04-03-2008 [eluser]Diego Pessoa[/eluser] Wow! Finally I found the soluction! =) Thanks a lot!!!! Now: Code: public static function getUsers() { Well, it hasn't a good looking, but works! hehe! =) Independent database - El Forum - 04-03-2008 [eluser]wiredesignz[/eluser] One question on this topic: Which rule states that you must use static method calls from within a class? I can see this is a practical issue where class methods are not in object context, but I don't see any difference in accessing the $CI instance (which brings you into object context) and using $this inside a Model, which is already in object context. |