Jump to content

Best way to retrieve multiple fields from another page?


Gabi
 Share

Recommended Posts

Good day, gentlemen,

I apologize if the answer to my question exists already, I just couldn't find it.

I'm working to have a group of global data such as tagline and contact info. I defined a hidden page ("sys") to store in its fields all the required data. Then the template (actually a prepended script) retrieves and attaches them to $page, like this:

$sys = wire('pages')->get(1222);

$page->tagline = $sys->headline;
$page->contact_name = $sys->contact_name;
$page->company_name = $sys->company_name;
$page->company_address = $sys->company_address;
$page->city = $sys->city;
$page->country = $sys->country;
$page->tel = $sys->tel;
$page->email = $sys->email;
$page->tel = $sys->tel;

However, I was surprized to see that using the "$page->get('selector')" is 3 times faster than "$page->selector" (even if we're talking about 30msec vs 90msec on localhost):

$sys = wire('pages')->get(1222);

$page->tagline = $sys->get('headline');
$page->contact_name = $sys->get('contact_name');
$page->company_name = $sys->get('company_name');
$page->company_address = $sys->get('company_address');
$page->city = $sys->get('city');
$page->country = $sys->get('country');
$page->tel = $sys->get('tel');
$page->email = $sys->get('email');
$page->tel = $sys->get('tel');
 

Wrong conclusion. I think I just witnessed the cache at work. They actually have the same speed.

I also thought of hard-coding them in /site/config.php. I know, not very clean and requires further work for multilingual data, but it seems it will be faster.

Another one would be a custom query to select all the data in one pass.

Which way would be the cleaner/faster/powerwirish one?

Thank you.

Link to comment
Share on other sites

Hi Gabi,

There's no difference between calling ->field or ->get('field');

First is routed through PHPs magic getter ( __get() ) and will call the get() method behind the scenes...

If you want to have global variables available all the time, why not just store them in a variable, e.g. $global?

No need to assign them to the actual $page variable:

// Store page in $global or $sys
$global = wire('pages')->get(1222);

// Later in any template... just get the values from your variable
echo "<div id='contact'>{$global->contact_name}</div>";
  • Like 2
Link to comment
Share on other sites

Hi Wanze,

Thanks for taking the time to answer my question.

Actually I knew about __get(), but my brain liked to believe half-baked measurements. I left the mistake in place, maybe it will be of some use for someone. 

I attached the variables to $page because at some point I used functions outside the template scope (I think) and I passed $page along with all variables to the said functions. But now I'm not using them anymore, so you're right, I don't need to assign them to $page.

I made some experiments and found that retrieving the variables directly from the database is only around 60 msec faster than your method on XAMPP for a page using the variables both in the header and in the footer. Your method is much more maintainable, so it stays.

All the best.

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...