Robin S Posted February 22, 2018 Share Posted February 22, 2018 A general coding question I've wondered about for a while but never got around to asking until now. Suppose I'm writing some code in a template file, and I get some value from the database... echo $page->parent->title; Now later on in my template I want to use this value again, so my template file is now... echo $page->parent->title; // More code here... echo $page->parent->title; Is PW going to go back to the database to get this value? Or is it automatically kept in memory so that only the first usage results in a trip to the database? In most circumstances where I do this I would tend to put the value in a variable and then reuse the variable... $parent_title = $page->parent->title; echo $parent_title; // More code here... echo $parent_title; ...but if I didn't and instead did it like the previous example, would there be any performance penalty? Is there any documentation or forum post that talks about how PW handles this sort of thing? 1 Link to comment Share on other sites More sharing options...
Zeka Posted February 22, 2018 Share Posted February 22, 2018 @Robin S I fas as know they are cached somehow, but I don't remember where I'he read it. Only first usage results in DB query. Actually, you can check this behavior by looking at PDO queries count in PW debug panel. Also would like to get more efficient answers. 1 Link to comment Share on other sites More sharing options...
kongondo Posted February 22, 2018 Share Posted February 22, 2018 Old topic below but might provide some insights 3 Link to comment Share on other sites More sharing options...
BitPoet Posted February 22, 2018 Share Posted February 22, 2018 14 hours ago, Robin S said: Is there any documentation or forum post that talks about how PW handles this sort of thing? Not sure about anything beside what @kongondo linked, but PW does definitely cache things, both field values and pages. Pages are kept in memory as any calls to Pages::get and Pages::find are routed through PagesLoaderCache. Equally, field values are retrieved only once, as Page::getFieldValue (which is in turn invoked by other get... methods like getUnformatted) only calls Fieldtype::loadPageField if the raw field value isn't yet populated (and $page->fieldname and $page->get('fieldname') both delegate their work to getFieldValue). 6 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now