Skip to content
Advertisement

How can I have a variable that all model can access in Codeigniter?

I want all record in a user request have the same date_created (even milisecond). I intend to set a variable in CI_Model, and when it’s loaded, all other Model can access it.

How can I do that? Thanks.

Advertisement

Answer

To create a global variable, you need to add a variable in application/config/constants.php.

See steps :

  1. Create your own file in application/libraries in which class constructor contains an array as an argument. Now create a new file in /application/config with same name as given in application/libraries and declare your global variables in it. Now to use these variables, autoload the newly created library.

  2. Create your own file in application/core and declare the global variables in it. Than in controller you need to extend your file name instead of CI_Controller.

  3. If the Global Variables are true constants, just add them in application/config/constants.php file and name them in all uppercase like the others are defined.

  4. If you want to set application constants create new config file and add the variables. Now load it as $this->config->load(‘filename’); And access those variables as

$this->config->date_created(‘variable_name’);

Now you can use this function in every model. Hope this will resolve your issue.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement