Remi Posted March 22, 2018 Posted March 22, 2018 Hi! I would like to build selector to get pages without children using one template. // Doesn't work: $selector = "template=invoice, children.template!=payment"; // Works but in opposite way I want: $selector = "template=invoice, children.template=payment"; // Works, but it's useless because "invoice" can have other templates as children $selector = "template=invoice, children.count=0"; // Code below works but it gets huge amount of data from DB first :/ $selector = "template=invoice"; $results = $pages->find($selector); foreach ($results as $result) { $hasPayments = $result->hasChildren("template=payment"); if (!$hasPayments){ $content .= "Blablabla"; } }
alxndre Posted March 22, 2018 Posted March 22, 2018 I'm on mobile, so I haven't tested this but can you try: $pages->findMany("template=invoice")->not("children.template=payment"); It should find all invoices first, and then filter out the results of your second selector which you say is the opposite of what you want. 1
Remi Posted March 22, 2018 Author Posted March 22, 2018 SOLUTION below Yep. It works. Corrected code: $selector = "template=invoice"; $results = $pages->find($selector)->not("children.template=payment"); // or: $results = $pages->find("template=invoice")->not("children.template=payment"); // Doesn't work correctly (doesn't exclude pages with children.template=payment (PW 3.0.62 & 3.0.96)): $results = $pages->findMany("template=invoice")->not("children.template=payment"); It also executes 0,5-2 times faster than first working script. I've tried to use findMany method, but it doesn't work with not method. 1
theo Posted March 22, 2018 Posted March 22, 2018 Doesn't it simply work like this? $selector = "template=invoice, !children.template=payment";
Remi Posted March 22, 2018 Author Posted March 22, 2018 4 minutes ago, theo said: Doesn't it simply work like this? $selector = "template=invoice, !children.template=payment"; I've tried that before. It doesn't return any results :/ But when I use this selector: $selector = "children.template!=payment"; I receive all pages without children using "payment" template.
theo Posted March 22, 2018 Posted March 22, 2018 Humm it works here, but there is one thing to be aware of afaics: As soon as there is "children.template=..." or "!children.template=..." in the second parameter, it does only list pages which actually have children. $selector = "template=invoice" may list more pages than $selector = "template=invoice, !children.template=atemplate"; even if none of the existing children have the "atemplate" template.
Remi Posted March 22, 2018 Author Posted March 22, 2018 6 minutes ago, theo said: As soon as there is "children.template=..." or "!children.template=..." in the second parameter, it does only list pages which actually have children. I think you are right.
alxndre Posted March 22, 2018 Posted March 22, 2018 55 minutes ago, Remi said: I've tried to use findMany method, but it doesn't work with not method. Maybe because findMany uses lazy loading and could not evaluate the results' children.template selector.
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