Jump to content

FindOne doesn't work with start


cpx3
 Share

Recommended Posts

Hi there,

to display a single slide from a huge database I only need a single page. But when I use the selector

$pages->findOne("template=template-news,start=1,sort=created")->title; 

the selector seems to ignore the start position and always returns the same page. As the docs specify clearly that findOne has the same options as find, I have been trying to find my mistake for the last four hours with any success... (I cannot use find as this slows down things extremly). Any ideas?

Link to comment
Share on other sites

Hi cpx3, if I understand correct, alternate you could try display the first 3 titles like this:

$news = $pages->find("template=template-news,start=1,sort=created")->slice(0, 2); // slice(start, limit)
foreach($news as $i){
echo $i->title;
}

first() would be also an option, getting the needed page -I think...
 

Link to comment
Share on other sites

If a find() is too slow, use findMany() with a suitable selector: https://processwire.com/api/ref/pages/find-many/.

Or if that's still too slow, or if you find them preferable for some other reason, use findJoin() or findRaw(), as described in the blog post at https://processwire.com/blog/posts/find-faster-and-more-efficiently/.

Another option would be to try RockFinder3: https://processwire.com/modules/rock-finder3/.

 

 

Link to comment
Share on other sites

You can do a find() but with a limit of 1. This will be just as efficient as findOne(), the only thing is that it will return a PageArray so use first() to get the item as a Page.

$results = $pages->find("template=template-news, sort=created, limit=1, start=1");
if($results->count) {
    $title = $results->first()->title;
}

 

Edited by Robin S
Corrected code
  • Like 1
Link to comment
Share on other sites

Thanks to everybody for the answer! I tried findRaw which seems to be the most effective but brings me to another problem: How to access page references? I have the following code:

$items = $pages->findRaw("template=template-news, select_region=austria, limit=2, objects=1", ["select_countries" => [ "id", "title" ]]);

where select_countries are referenced pages. But I have no clue how to access the select_countries id as it neither works with array[0] nor with select_countries->title... 

Link to comment
Share on other sites

4 hours ago, cpx3 said:

How to access page references? I have the following code:

$items = $pages->findRaw("template=template-news, select_region=austria, limit=2, objects=1", ["select_countries" => [ "id", "title" ]]);

where select_countries are referenced pages. But I have no clue how to access the select_countries id as it neither works with array[0] nor with select_countries->title...

See if this topic helps? I've had issues with that findRaw page reference syntax in the past; unsure if it has been fixed in later PW versions as discussed in that topic.

 

Link to comment
Share on other sites

It seems that, as @kongondo notes, there's an issue with the findRaw() syntax, at least with the array-type syntax and page-reference fields.

However, the dot syntax seems to work. So I think the following will give you what you need:

$items = $pages->findRaw("template=template-news, select_region=austria, limit=2, objects=1", ["select_countries.id", "select_countries.title"]); 

Or slightly simpler, as the keys of the returned array of page references are the page ids:

$items = $pages->findRaw("template=template-news, select_region=austria, limit=2, objects=1", ["select_countries.title"]); 

I've submitted an issue regarding the other syntax: https://github.com/processwire/processwire-issues/issues/1563

 

  • Like 2
Link to comment
Share on other sites

Thanks once more for the answers! My problem is that I have no idea how to get the value of select_countries.title (which is a referenced page). I tried (beside many variations):

foreach ($items as $p) {
   echo $p->select_countries.id;
}

But with no results...

Link to comment
Share on other sites

Using findRaw() you get a normal PHP array (not a PW page array). And the value for select_countries is itself an array. So you can do this:

foreach($items as $item) {
    foreach($item['select_countries'] as $country) {
        echo $country['title'];
    }
}

I'm not sure what findRaw() returns if your page selector field is set to a single page, but you may need to simply use $item['select_countries']['title'] rather than the inner foreach.

 

  • Like 1
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...