Jump to content

Pages' find method returns an ID


cryostar
 Share

Recommended Posts

When I read the docs it says that the return type of the find method is a PageArray object.

echo $pages->find("template=category, categorycode=ABCD12345"); //returns 1064

However, when I checked it's returning an ID. Which explains why I cannot retrieve the page's fields either by `->get('field')` or `->field`. Am I missing something or was I looking at a different API?

Link to comment
Share on other sites

I got it! I need to use `get` instead of `find` for single pages. That's why I couldn't access it! Odd though, I set "findOne=true" in the options parameter as well... I was hoping that it would return a Page object when I set it.

Link to comment
Share on other sites

An alternative would also be findOne

Quote

Like find() but returns only the first match as a Page object (not PageArray).

This is functionally similar to the get() method except that its default behavior is to filter for access control and hidden/unpublished/etc. states, in the same way that the find() method does. You can add an include=... to your selector string to bypass. This method also accepts an $options array, whereas get() does not.

 

  • Like 1
Link to comment
Share on other sites

On 6/20/2021 at 3:23 PM, cryostar said:

I got it! I need to use `get` instead of `find` for single pages. That's why I couldn't access it! Odd though, I set "findOne=true" in the options parameter as well... I was hoping that it would return a Page object when I set it.

It does return a Page object, but if you use echo on it the output is just the ID:

$notices = $pages->find("id=1107");
echo "echo: " . $notices;
echo "<br> print_r: ";
print_r($notices);

Result:

echo: 1107
print_r: ProcessWire\PageArray Object ( [count] => 1 [items] => Array ( [Page:0] => Array ( [id] => 1107 [name] => access-on-friday [parent] => /news-notices/notices/ [template] => notification [title] => Access on 13/8 [item_date] => 2021-06-15 00:00:00 ) ) [selectors] => id=1107 )

 

Link to comment
Share on other sites

25 minutes ago, kp52 said:

It does return a Page object, but if you use echo on it the output is just the ID:

 

That is the output of a PageArray, try making a find() that finds more than one page, and you''ll see what I mean! It would render something like this:

1107|1200|1201|1021

So, since you only find ONE page, it only outputs that page, but it is still an instance of a PageArray object getting echoed, and the echo outputs like the example. 

Take a look a this line of the PageArray class:

https://github.com/processwire/processwire/blob/master/wire/core/PageArray.php#L701

And I'll venture to say this has to do with this magic method:

https://www.php.net/manual/en/language.oop5.magic.php#object.tostring

Link to comment
Share on other sites

5 hours ago, kp52 said:

Absolutely, it even says so in the output. Doh! My point was, though, that what is returned isn't just an ID.

Oh yes of course, sorry! Didn't mean to sound condescending was just trying to elaborate for @cryostar! I got confused into who I was answering.

Link to comment
Share on other sites

Thank you all for helping me understand this. I thought that when I echoed an object it returns the object type. I got confused why it returned a string ?. (I'm just a returning PHP programmer, so please bear with me ?)

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...