Welcome Guest, Not a member yet? Register   Sign In
Repeating DB-Model Functions without calling the Database more than once
#1

[eluser]Polarity[/eluser]
Hello,i need a tipp from the community. So heres my problem.

Say i got this User Model with a function that retrieves User Data from the Database. To print 2 Values i have to write:

Code:
$data = $this->user_model->findId(1);

print $data->username;
print $data->website;

but i want write something like this:

Code:
print $this->user_model->findId(1)->username;
print $this->user_model->findId(1)->website;

thats also no problem. but ci calls the database 2 times.
how can i prevent this without caching and without modifing the model (i got many of them already)
#2

[eluser]imn.codeartist[/eluser]
better stick with the first one
#3

[eluser]bretticus[/eluser]
In theory, you could store a "row variable" in an array with the associative key as the id you are sending. If you get an empty result when checking the array, perform the query. If there's something there, return the value. You'd need to store the array as a private property in the model class so it is accessible on subsequent calls. Of course once the instance is destroyed (page is sent and buffers cleared or execution stops) that array is also cleaned up.




Theme © iAndrew 2016 - Forum software by © MyBB