Welcome Guest, Not a member yet? Register   Sign In
Model Static variable reset on page refresh
#1

[eluser]Unknown[/eluser]
Hello,

I have a problem in my application using CodeIgniter.

This is what I want:
In my model I make a select all from a table on data base. In an attempt to improve the app perform I planned to make this selection only once and store the result array in an static variable, this way, this variable will be returned in following calls.

This is the problem:
Every time I refresh the page my model is reloaded and my static variable receives null, this way, every time the data base select is done.

How can I resolve this??

Model:
Code:
class Account extends CI_Model {
     private static $_allAccountsArray = null;  
    public function __construct()  {
        log_message("DEBUG", ">> Loading Account");
    }

    public function selectAll ()     {
         if ( self::$_allAccountsArray === null ) {
             log_message("DEBUG", "allAccountsArray is null");
             $count=0;
             $sqlAccounts = "SELECT * FROM ACCOUNT";

             $conn = $this->db2->getInstance();
             $result = db2_exec($conn, $sqlAccounts);
             if ($result) {
                    while ($row = db2_fetch_assoc($result)) {
                        self::$_allAccountsArray[$count++] = $row;
                    }
               } else {
                    log_message("ERROR", db2_stmt_error()."-".db2_stmt_errormsg());
               }  
          }
          return self::$_allAccountsArray;    
       }
}

My log:
DEBUG - 2014-09-25 00:01:24 --> Config Class Initialized
DEBUG - 2014-09-25 00:01:24 --> Hooks Class Initialized
DEBUG - 2014-09-25 00:01:24 --> Utf8 Class Initialized
DEBUG - 2014-09-25 00:01:24 --> UTF-8 Support Enabled
DEBUG - 2014-09-25 00:01:24 --> URI Class Initialized
DEBUG - 2014-09-25 00:01:24 --> Router Class Initialized
DEBUG - 2014-09-25 00:01:24 --> Output Class Initialized
DEBUG - 2014-09-25 00:01:24 --> Security Class Initialized
DEBUG - 2014-09-25 00:01:24 --> Input Class Initialized
DEBUG - 2014-09-25 00:01:24 --> Global POST and COOKIE data sanitized
DEBUG - 2014-09-25 00:01:24 --> Language Class Initialized
DEBUG - 2014-09-25 00:01:24 --> Loader Class Initialized
DEBUG - 2014-09-25 00:01:24 --> Helper loaded: url_helper
DEBUG - 2014-09-25 00:01:24 --> Session Class Initialized
DEBUG - 2014-09-25 00:01:24 --> Helper loaded: string_helper
DEBUG - 2014-09-25 00:01:24 --> Session routines successfully run
DEBUG - 2014-09-25 00:01:24 --> Controller Class Initialized
DEBUG - 2014-09-25 00:01:24 --> Model Class Initialized
DEBUG - 2014-09-25 00:01:24 --> >> Loading Account
DEBUG - 2014-09-25 00:01:24 --> allAccountsArray is null
DEBUG - 2014-09-25 00:01:24 --> File loaded: Deliverable/application/views/deliverable/index.php
DEBUG - 2014-09-25 00:01:24 --> File loaded: Deliverable/application/views/templates/Default.php
DEBUG - 2014-09-25 00:01:24 --> Final output sent to browser
DEBUG - 2014-09-25 00:01:24 --> Total execution time: 0.2245
DEBUG - 2014-09-25 00:01:46 --> Config Class Initialized
DEBUG - 2014-09-25 00:01:46 --> Hooks Class Initialized
DEBUG - 2014-09-25 00:01:46 --> Utf8 Class Initialized
DEBUG - 2014-09-25 00:01:46 --> UTF-8 Support Enabled
DEBUG - 2014-09-25 00:01:46 --> URI Class Initialized
DEBUG - 2014-09-25 00:01:46 --> Router Class Initialized
DEBUG - 2014-09-25 00:01:46 --> Output Class Initialized
DEBUG - 2014-09-25 00:01:46 --> Security Class Initialized
DEBUG - 2014-09-25 00:01:46 --> Input Class Initialized
DEBUG - 2014-09-25 00:01:46 --> Global POST and COOKIE data sanitized
DEBUG - 2014-09-25 00:01:46 --> Language Class Initialized
DEBUG - 2014-09-25 00:01:46 --> Loader Class Initialized
DEBUG - 2014-09-25 00:01:46 --> Helper loaded: url_helper
DEBUG - 2014-09-25 00:01:46 --> Session Class Initialized
DEBUG - 2014-09-25 00:01:46 --> Helper loaded: string_helper
DEBUG - 2014-09-25 00:01:46 --> Session routines successfully run
DEBUG - 2014-09-25 00:01:46 --> Controller Class Initialized
DEBUG - 2014-09-25 00:01:46 --> Model Class Initialized
DEBUG - 2014-09-25 00:01:46 --> >> Loading Account
DEBUG - 2014-09-25 00:01:46 --> allAccountsArray is null
DEBUG - 2014-09-25 00:01:46 --> File loaded: Deliverable/application/views/deliverable/index.php
DEBUG - 2014-09-25 00:01:46 --> File loaded: Deliverable/application/views/templates/w3Default.php
DEBUG - 2014-09-25 00:01:46 --> Final output sent to browser
DEBUG - 2014-09-25 00:01:46 --> Total execution time: 0.2424
#2

[eluser]CroNiX[/eluser]
I'm not clear on what you're asking. Of course it gets reset to null when you refresh the page as it's a new request to the server.

If you want it to always be populated, maybe you just need to run $this->selectAll() in the __construct. Of course you'll still get the null message in the logs once for each request where that model gets loaded.
#3

[eluser]LuckyFella73[/eluser]
For me it sounds like is looking for db caching.




Theme © iAndrew 2016 - Forum software by © MyBB