![]() |
Autoload model with a different object name - 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: Autoload model with a different object name (/showthread.php?tid=17508) |
Autoload model with a different object name - El Forum - 04-07-2009 [eluser]RJ[/eluser] Gm, Is it possible to auto a model and assign it a different object name? In a controller I would do this: Code: $this->load->model('Model_name', 'fubar'); In autoload the second param would be another model name. Code: $autoload['model'] = array('Common'); Thank you. Autoload model with a different object name - El Forum - 04-07-2009 [eluser]TheFuzzy0ne[/eluser] That's a very good question, and unfortunately there's no way to do that. As far as I can see, this leaves two options. Option 1 - You load a helper file or something similar, which will reassign the autoloaded model to a name of your liking. Perhaps something like this: Code: function reassign_model_names() ...But that's fugly, and clunky. Option 2 - Override the Loader library with your own method: ./system/application/libraries/MY_Loader.php # CODE MOVED TO http://ellislab.com/forums/viewthread/110977/#560058 The code above is untested, but should allow you to use an array in your autoloader: Code: $autoload['libraries'] = array('database', array('email', NULL, 'email2'), 'session'); The arguments in the array should be just as they would be if you were to call $this->load->library() directly: Code: array($library_name, $params, $object_name) Hope this helps. Autoload model with a different object name - El Forum - 04-07-2009 [eluser]TheFuzzy0ne[/eluser] Ah nuts. I got lost there, and this will only allow you to load libraries in that fashion, not models... Sorry. I'll go back to the drawing board soon. Autoload model with a different object name - El Forum - 04-07-2009 [eluser]wiredesignz[/eluser] Try this method in a CI_Loader class extension Code: /* autoload models */ Autoload model with a different object name - El Forum - 04-07-2009 [eluser]TheFuzzy0ne[/eluser] OK, I started over, and I noticed that the model and library loader methods worked a lot more differently to each other than I'd thought, so I decided to override both the model() and library() methods in the loader class. I figured that you might as well allow the same behaviour with libraries too, although you are free to delete that method if you wish. I also think I might have spotted a potential bug in the library loader, but I need to look into that a bit further. ./system/application/libraries/MY_Loader.php Code: <?php Autoload model with a different object name - El Forum - 04-07-2009 [eluser]RJ[/eluser] Close! Line 37 needs ")" and I get this error: Quote:Message: Missing argument 1 for Dealer_model: On this bit of code: Code: $CI->$name = new $model(); Off of this auto load: Code: $autoload['model'] = array(array('dealer_model', NULL, 'dealer')); Autoload model with a different object name - El Forum - 04-07-2009 [eluser]TheFuzzy0ne[/eluser] You are using the syntax for loading a library. In your autoload.php file, to load a model you'd need to do something like this: Code: $autoload['model'] = array(array('dealer_model', 'dealer')); Autoload model with a different object name - El Forum - 04-07-2009 [eluser]RJ[/eluser] This is the method in my main controller calling the deal model and passing $dealer from the uri. Not passing right for some reason Code: function device($dealer = '999') This is my dealer_model: Code: class Dealer_model extends Model { Autoload model with a different object name - El Forum - 04-07-2009 [eluser]TheFuzzy0ne[/eluser] I've just borked my Aptana install, so I'm reinstalling it now. I'll do some debugging shortly. Autoload model with a different object name - El Forum - 04-07-2009 [eluser]RJ[/eluser] Ok. So I can access the model with this: Code: $this->dealer->dealer_model($dealer); Not entirely sure why I need to name the constructor for my dealer_model, but it doesnt work without that. I can't get my controller to send the URI data to the model; had a similar issue with a library, fixed it, but i decided to use a model rather than a library, back at the same problem. Controller: Code: function Shop() Dealer_model: Code: class Dealer_model extends Model { PHP ERRORS: Code: list($model, $name, $db_conn) = $babe; Quote:Severity: Notice --> Undefined offset: 2 W:\www\s\system\application\libraries\FW_Loader.php 67there are more, but those should be solved once the passing from controller to model is worked out. |