Calling CI models from outside script in CI 2 |
[eluser]Unknown[/eluser]
I would like to use my CI models in a php script. This script won't be accessible from anywhere but only to admins on the server itself. This will be a one time initialization type script. I would be very easy if I had access to my models. I came across this on github: https://github.com/EllisLab/CodeIgniter/...ide-script It is obviously outdated, also doesn't mention whether it will work when the models are extended by MY_Model, or when the core controller is extended. I've been digging in to the CI core trying to figure out how to port this over to CI 2 using model and controller extensions and just when I thought I was getting somewhere I got the same error I'd been getting from the beginning. "Unable to locate the specified class: Controller.php" Here's my modified ci_model_remote_open.php. Has anyone done this? I'm also open to a better solution to calling a one time php script that interacts with CI. Thanks! Code: <?php
[eluser]Unknown[/eluser]
Ok I finally got this working. The problem in my case seemed to be with the core. in Common.php the problem is very well hidden. It turns out if the call to is_loaded($class) comes before the call $_classes[$class] = new name(); the call to construct the new controller fails. I'm not really sure why however the output clearly shows that more objects are constructed when the is_loaded($class) comes after instantiating the new objects with the line $_classes[$class] = new name(); example 1) Output of the _is_loaded array with code as is (fails to load controller properly): Code: _is_loaded-->Array Code: _is_loaded-->Array It would appear that by instantiating the controller object first we get the loader and model class inserted in when is_loaded('Controller') is called. I can achieve the same thing by instantiating the class in my script (ci_model_remote_open.php) rather than running load_class. example (refer to my first post for full file, ci_model_remote_open.php) Code: /* However as you can see it is not as reusable because I have to call "MY_Controller" specifically rather than the generic "Controller" that load_class allows. So is there any reason that is_loaded($class) should come before $_classes[$class] = new name();? I think I have given at least one reason why it shouldn't. Below is the hack to the core that I made. Code: $_classes[$class] = new $name(); Anyone see any problems with this? Issues it might cause down the road? Full function load_class (only piece of core I had to modify to get remote scripts working) from Common.php below... Code: // ------------------------------------------------------------------------
[eluser]ivantcholakov[/eluser]
A similar trick (with using a fake controller) is possible not only for models, but for everything - models, libraries, helper functions, accessing configuration options. It is convenient having such a feature for refactoring old, non-MVC sites. |
Welcome Guest, Not a member yet? Register Sign In |