![]() |
Loading multiple models in a controller, which extend the same base class. - 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: Loading multiple models in a controller, which extend the same base class. (/showthread.php?tid=18473) |
Loading multiple models in a controller, which extend the same base class. - El Forum - 05-07-2009 [eluser]Nick Jennings[/eluser] Hello, I have 2 tables in a database, 'website' and 'client'. Each table has it's own model which extends a "base" class (MA_Model) - this base class provides methods which are identical between the two models (things like generic select *'s and getting a record based on ID, etc.). This works fine, until I try to load both the 'website' and 'client' models in the same controller function. When I try to load the second model, I get the following error in the apache logs: Code: PHP Fatal error: Cannot redeclare class MA_model in <ci_path>/models/ma_model.php on line XXX, (NOTE: line XXX always equals the closing bracket of the MA_Model class definition ie. end of file) I'm not sure why I can extend the base CI 'Model' class for every class I make, however when I make my own 'MA_Model' class, I can't load two models at once which both extend the class. Am I doing something wrong? Here is a snippet of my code (simplified for purpose of posting): controllers/website.php Code: <?php models/website_model.php Code: <?php models/ma_base.php Code: <?php Thanks in advance for any help! -Nick Loading multiple models in a controller, which extend the same base class. - El Forum - 05-07-2009 [eluser]drewbee[/eluser] You can extend the base model class as many times as you like because it is only been declared once, but initiated many times. Declared = file has been loaded through a require/include etc. Initiated = class has been initiated into an instance of the class and assigned a var. For your include, you simply need to check and see if the class has been loaded yet: I believe class_exists checks against declared classes, so you should be able to use that: replace: Code: include dirname(__FILE__)."/ma_model.php"; With: Code: if (!class_exists('MA_Model', false)) Now you should just have to put this on each of your files where you want to use multiple models in the same controller. |