-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Calling CI models from outside script
Warning: I just put this here since it was to big and couldn't fit it into a post. If it turns out it's ok it might remain here.
The system consists of 3 files.
[b]config_constants.php:[/b] [code] <?php /**
- This file is pretty much index.php with a few modifications
- which are:
-
- the loading of the front controller is removed from the bottom since
-
this file is now included from ci_model_remote_open.php
-
- when setting $system_folder and $application_folder we check for previous
- existence of those variables so we can set them previous to including
- ci_model_remote_open.php. This way it's more flexible. */
//--------------------------------------------------------------------------------------
/* |
---|
PHP ERROR REPORTING LEVEL |
--------------------------------------------------------------- |
| | By default CI runs with error reporting set to ALL. For security | reasons you are encouraged to change this when your site goes live. | For more info visit: http://www.php.net/error_reporting | */ error_reporting(E_ALL);
/* |
---|
SYSTEM FOLDER NAME |
--------------------------------------------------------------- |
| | This variable must contain the name of your "system" folder. | Include the path if the folder is not in the same directory | as this file. | | NO TRAILING SLASH! | */ if( ! $system_folder) $system_folder = "system";
/* |
---|
APPLICATION FOLDER NAME |
--------------------------------------------------------------- |
| | If you want this front controller to use a different "application" | folder then the default one you can set its name here. The folder | can also be renamed or relocated anywhere on your server. | For more info please see the user guide: | http://www.codeigniter.com/user_guide/general/managing_apps.html | | | NO TRAILING SLASH! | */ if( ! $application_folder) $application_folder = "application";
/* |=============================================================== | END OF USER CONFIGURABLE SETTINGS |=============================================================== */
/* |
---|
SET THE SERVER PATH |
--------------------------------------------------------------- |
| | Let's attempt to determine the full-server path to the "system" | folder in order to reduce the possibility of path problems. | */ if (function_exists('realpath') AND @realpath(dirname(FILE)) !== FALSE) { $system_folder = str_replace("\", "/", realpath(dirname(FILE))).'/'.$system_folder; }
/* |
---|
DEFINE APPLICATION CONSTANTS |
--------------------------------------------------------------- |
| | EXT - The file extension. Typically ".php" | FCPATH - The full server path to THIS file | SELF - The name of THIS file (typically "index.php) | BASEPATH - The full server path to the "system" folder | APPPATH - The full server path to the "application" folder | */ define('EXT', '.'.pathinfo(FILE, PATHINFO_EXTENSION)); define('FCPATH', FILE); define('SELF', pathinfo(FILE, PATHINFO_BASENAME)); define('BASEPATH', $system_folder.'/');
if (is_dir($application_folder)) { define('APPPATH', $application_folder.'/'); } else { if ($application_folder == '') { $application_folder = 'application'; }
define('APPPATH', BASEPATH.$application_folder.'/');
}
/* |
---|
DEFINE E_STRICT |
--------------------------------------------------------------- |
| | Some older versions of PHP don't support the E_STRICT constant | so we need to explicitly define it otherwise the Exception class | will generate errors. | */ if ( ! defined('E_STRICT')) { define('E_STRICT', 2048); }
?>
[/code]
[b]ci_model_remote_open.php:[/b] [code] <?php /**
- This file is just a cannibalization of "system/codeigniter/CodeIgniter.php"
- where everything that is non-essential to model loading has been removed (I hope)
- Instead of loading a controller and letting it take control, we instantiate the
- base Controller class and then include the base Model class.
- This way, we should be ready for an external program instantiating a ci model class
- after including this. */
//--------------------------------------------------------------------------------------
/*
-
- Load constants
-
*/ require_once('config_constants.php');
/*
-
- Load the global functions
-
*/ require(BASEPATH.'codeigniter/Common'.EXT);
/*
-
- Define a custom error handler so we can log PHP errors
-
*/ set_error_handler('_exception_handler'); set_magic_quotes_runtime(0); // Kill magic quotes
/*
-
- Instantiate the base classes
-
*/
$CFG =& load_class('Config'); $LANG =& load_class('Language');
/*
-
- Load controller
-
- Note: Due to the poor object handling in PHP 4 we'll
- conditionally load different versions of the base
- class. Retaining PHP 4 compatibility requires a bit of a hack.
- Note: The Loader class needs to be included first
*/ if (floor(phpversion()) < 5) { load_class('Loader', FALSE); require(BASEPATH.'codeigniter/Base4'.EXT); } else { require(BASEPATH.'codeigniter/Base5'.EXT); }
// instantiate a "fake" controller $CI = load_class('Controller');
/*
-
- Prepare for model instantiation
-
*/
// load Model parent class require_once(BASEPATH.'libraries/Model'.EXT);
?> [/code]
[b]ci_model_remote_close.php[/b] [code] <?php /**
- Again, cannibalized from the end of "system/codeigniter/CodeIgniter.php",
- this takes care of cleaning up after a model */
//--------------------------------------------------------------------------------------
/*
-
- Close the DB connection if one exists
-
*/ if (class_exists('CI_DB') AND isset($CI->db)) { $CI->db->close(); }
?> [/code]
And the following is a sample usage inside a script (although it's kind of raw): [code] <?php
// open the model remote $system_folder = "CodeIgniter/system"; require_once('ci_model_remote_open.php');
// load my report model require_once(APPPATH . "/models/foo_model.php");
// instantiate model $foo = new Foo_model(); $foo->_assign_libraries(); // database stuff won't work without this
// call function print "
"; print_r($foo->getFooDataset(47, 'bobo')); print "";
// close the model remote require_once('ci_model_remote_open.php');
?> [/code]
Notice that you are not restricted even in loading a model from the application/models folder. You can even create models somewhere outside that inherit from Model in the standard CI way and they will know how to use the database defined in CI and do the usual stuff. Also, the example above could probably have been into a function to make it cleaner. What do you think?