Jump to content

PHP function arguments and PageArrays...


celfred
 Share

Recommended Posts

Hello,

Another newbie question. My Processwire sites is growing and I'm wondering if my way of doing things sounds goog to you. I tend to avoid what I consider 'heavy and frequent' database requests in my functions. For example :

// In functions.php
myfunction($player) {
  wire('$pages')->find("myselectors"); // This means about 300 database requests
  do something... (like $p->newTmpField = 1;)
  return $player;
}

// In template.php
$allPlayers = $pages->find("template=player"); // About 300 players
foreach ($allPlayers as $p) {
  myfunction($p);
}
// Now I have access to newTmpField, for example.

 I tend to replace with :

// In function.php
myfunction($allPlayers) {
  foreach($allPlayers as $p) {
    // do something like set $p->newTmpField = 1
  }
  return $allPlayers;
}

// In template.php
$allPlayers = $pages->find("template=player");
myfunction($allPlayers);
// Now $players have been modified. For example, I have $allPlayer->find("newTmpField=1");

So for the moment, I tend to choose the second solution which makes only 1 database request, but what do more experimented developers would recommend ?

What I find useful with Page objects in functons arguments is that I have the whole 'tree' I can then 'find' in my function. But as you can see, I need to understand things better. I have a feeling my 'practical' explanation is not really convincing 🙂 

Thanks if someone can take a few minutes to give me advice !

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...