PWaddict Posted January 30, 2017 Share Posted January 30, 2017 I display a button ONLY if there are more than 3 children on a specific template. So, I'm doing this: $posts = $pages->find('template=posts-content'); if(count($posts) > 3) { echo "Button"; } On some point that template will have hundreds of children. Is the above method ok or should I use findMany? $posts = $pages->findMany('template=posts-content'); if(count($posts) > 3) { echo "Button"; } or is it better to just use a limit on the selector? $posts = $pages->find('template=posts-content, limit=4'); if(count($posts) > 3) { echo "Button"; } Link to comment Share on other sites More sharing options...
adrian Posted January 30, 2017 Share Posted January 30, 2017 $pages->count('template=posts-content'); 4 Link to comment Share on other sites More sharing options...
kongondo Posted January 30, 2017 Share Posted January 30, 2017 Just a bit of an explanation for the next guy....: 4 hours ago, PWaddict said: $posts = $pages->find('template=posts-content'); This will retrieve the pages, return a PageArray (PHP Object), which might end up using a lot of memory. Instead what you want is just the count of pages... 2 hours ago, adrian said: $pages->count('template=posts-content'); Which brings us to this. This is very efficient. It will only count these pages and return an integer, not only doing its job very quickly but using next-to-nothing memory. 7 Link to comment Share on other sites More sharing options...
Zeka Posted January 30, 2017 Share Posted January 30, 2017 Hi @kongondo Are these two methods the same from the point of performance? $pages->find('template=posts-content')->count(); $pages->count('template=posts-content'); Link to comment Share on other sites More sharing options...
kongondo Posted January 30, 2017 Share Posted January 30, 2017 @Zeka, No is the answer 9 minutes ago, Zeka said: $pages->find('template=posts-content')->count(); This will first return the PageArray (i.e. load pages) and then do an in-memory count. More like counting items in an array. 9 minutes ago, Zeka said: $pages->count('template=posts-content'); This will do the counting at the db-level, hence faster and more efficient if all you want to do is count. 5 Link to comment Share on other sites More sharing options...
Zeka Posted January 30, 2017 Share Posted January 30, 2017 @kongondo Thank you for explanation Link to comment Share on other sites More sharing options...
SamC Posted January 31, 2017 Share Posted January 31, 2017 Interesting, I didn't know this existed. Good explanation too @kongondo thanks. 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