bwakad Posted June 20, 2014 Share Posted June 20, 2014 I would like to test if a certain single page exist... using a switch (true or false) or if statement. I can use $pages->find("/$contract/") or $pages->get("/$contract/"). find() is returning a pageArray, get() is returning a page or NULL. So which one is better to test? Is a NULL page still evaluating to true?! Link to comment Share on other sites More sharing options...
Soma Posted June 20, 2014 Share Posted June 20, 2014 better count than find if($pages->count("/contract/")){ // exists } with find() it would be slightly less efficient if($pages->find("/contract/")->count){ // exists } Or get() will return a NullPage object if not found if(!pages->get("/contract/") instanceof NullPage){ // exists } 1 Link to comment Share on other sites More sharing options...
bwakad Posted June 20, 2014 Author Share Posted June 20, 2014 Then for accuracy count (your first example) is better. Thanks Soma! Probably there is no way to make this shorter: $contract = $sanitizer->text($input->post->contract); if ($pages->count("/contract/{$contract}")) { Link to comment Share on other sites More sharing options...
bwakad Posted June 20, 2014 Author Share Posted June 20, 2014 Well, still having trouble with this... my default option for a dropdown is this: <option value=''>Any</option> if ($input->post->submit) { if ( empty($input->post->contract) ) { echo "hello" ; } // is returning hello when "Any" is submitted. So we know it is empty! echo $input->post->contract; // is returning the value if other option then "Any" is chosen. $contract = $sanitizer->text($input->post->contract); echo $contract; // is returning the value if other option then "Any" is chosen if($pages->count("/contract/{$contract}")) echo "found"; // this is wrong, it returns "found" with option "Any" or another value chosen. Empty or not! And that last part I can't find out what's wrong, since clearly the page "Any" is not existing... Link to comment Share on other sites More sharing options...
Soma Posted June 20, 2014 Share Posted June 20, 2014 At what value is "Any"? If it's empty then "/contract/" does exist. Link to comment Share on other sites More sharing options...
bwakad Posted June 20, 2014 Author Share Posted June 20, 2014 It's just an empty name used in the <option> and not a page. The directory I check is /contract/$contract where $contract is the $input->post->contract <option> selected Maybe that's the issue: if $contract is empty, it checks the /contract/ which does exist... hmmm how to do this? -- EDIT -- $pages->count("/contract/, child={$contract}"); works. Not sure if this is the fastest way though... not working Link to comment Share on other sites More sharing options...
diogo Posted June 20, 2014 Share Posted June 20, 2014 You can also do if($pages->get("/contract/")->id){ // exists } Link to comment Share on other sites More sharing options...
bwakad Posted June 20, 2014 Author Share Posted June 20, 2014 The problem is not the /contract/ but the $contract I need to check. So $contract represents a child page name in string format parent: contract child: $contract path: /contract/$contract Link to comment Share on other sites More sharing options...
diogo Posted June 21, 2014 Share Posted June 21, 2014 $pages->get("parent=/contract/, name=$contract") You might consider changing the name of the parent to contracts Link to comment Share on other sites More sharing options...
bwakad Posted June 21, 2014 Author Share Posted June 21, 2014 $pages->get("parent=/contract/, name=$contract") You might consider changing the name of the parent to contracts Thanks Diogo / Soma ! I had to combine your replies into 1, or combine Soma's with your's. To make this short, I am testing to see if a page really exist. Using get I need to add ->id, otherwise I am getting a false positive/negative (returning NullPage). Using count() without ->id, otherwise that last is property of non-object (if page exist or not). get() with id if($pages->get("parent=/contracts/, name={$contract}")->id) { echo "found"; } else { echo "not found"; } count() without id if($pages->count("parent=/contracts/, name={$contract}")) { echo "found"; } else { echo "not found"; } Link to comment Share on other sites More sharing options...
Martijn Geerts Posted June 21, 2014 Share Posted June 21, 2014 There's a difference between the get and the count. Count uses the find methode internally, thus it won't count the page if it is in a hidden state. But the get will regardless of the state. Correct me here if i'm wrong. Link to comment Share on other sites More sharing options...
bwakad Posted June 21, 2014 Author Share Posted June 21, 2014 Hey Martijn! Count() and find() does not return NullPage is not exist. Get() does. Unless you add specificly ->id But for testing, between count() and find(), the API states count() just returns the quantity, and does not load the page (if it does find). Link to comment Share on other sites More sharing options...
Martijn Geerts Posted June 21, 2014 Share Posted June 21, 2014 @bwaked: take a look at this // here get() will get the page, while count, does not count it. parent page | +- page (status=hidden) (still I could be wrong socorrect me when wrong, no computer to test it right now) Link to comment Share on other sites More sharing options...
AndreasWeinzierl Posted August 7, 2018 Share Posted August 7, 2018 so the most efficient query would still be some form of: if($pages->count("/contract/")){ // exists } ? 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