I’m using CodeIgniter.
Let’s say I have a model for the current user and a model for an userlist. In the model for the userlist I cache the WHERE statements because if I don’t, I have to apply them all again after the use of $this->db->count_all_results (that function resets active record functions). I off course need those also for the $this->db->get(‘users’).
The functions in the userlist model to filter users look like this:
public function onlyWomen()
{
$this->db->start_cache();
$this->db->where('gender', '1');
$this->db->stop_cache();
}
Problem is that i’m also working with a model for the current user. I suspect the cache I set in the userlist model to also copy to de current user model (i’m working with both at the same time, so I can’t flush the cache when starting with the current user model). I thought the active record cache should stay in the object in wich it is called.
How do I circumvent or resolve this?
(My English ain’t that bad, but in combination with this complicated stuff and me maybe not fully understanding objects/classes, I hope you understand my problem ^^)
CodeIgniter effectively creates singletons when you load classes using the built in Loader class (for example, using
$this->load->database()or$this->load->model('user')). This is why active record caching remains persistent between your Userlist and User models. There is no out of the box way to circumvent this. You would have to edit core files to implement a solution.Instead of creating methods for building queries through caching, why don’t you create methods for building and executing queries? Here is an example of what I mean:
Edit: Adding an example of using optional parameters for specifying where clauses.
Obviously it can get a little more complicated but that is the general idea.