Jump to content

Finding by value in a pageField of a PageArray


thomas
 Share

Recommended Posts

Hello,

I have a fieldtype Page named 'category' in my pages. I want to find pages from several categories like this:

$teaser=$pages->find("template=video,category.id=17|19|23,sort=-created");

This works great. Now I try the same thing on "my own" pageArray:

$sql = "select distinct video from views order by created desc";
$result = wire('db')->query($sql);
$bw = new PageArray;
while($row = $result->fetch_array()) {
if($video = $pages->get($row['video'])->id) $bw->add($video);
}

$teaser=$bw->filter("template=video,category.id=17|19|23");

... now $teaser contains nothing. It works well when I leave out "category.id=[...]" in the filter ...

Can anyone help?

Thanks,

thomas

Link to comment
Share on other sites

I'm a little confused as to what you're doing here :)

Why are you querying the db manually? Those fields and table are custom and video is the id of a page in PW?

Well does the db query return any result at all? Any errors?

What if you leave out the .id in category.id? Have you tried a fiend instead of a filter?

Link to comment
Share on other sites

Hi Soma,

"views"is a simple table which writes a page id whenever someone watches a video on my site. I expect this to get rather large so I didn't want it to be PW pages. The query returns an array of page id's and works fine. As I said, everything works fine until I add "category.id" to filter (or "find", I tried both) ... I'll try leaving out the .id ...

Thanks,

thomas

Link to comment
Share on other sites

Subfields like "category.id" aren't currently supported by the PageArray in-memory find/filtering functions. Though I think I can add this easily enough. However, "category" should resolve to the id property anyway, so you probably don't need it.

Link to comment
Share on other sites

Thanks for the info Ryan,

I now changed the code to this:

while($row = $result->fetch_array()) {
if($video = $pages->get("id=".$row['video'].",category.id=17|23|9")->id) $bw->add($video);
}

thus avoiding the problem. Not sure if this is better or worse in performance terms but at least it works ...

- thomas

Link to comment
Share on other sites

This might be a more efficient way to go, by converting multiple $pages->get() calls into 1 $pages->find() call.

$ids = array();
while($row = $result->fetch_array()) $ids[] = $row['video']; 
$bw = $pages->find("id=" . implode('|', $ids) . ", category=17|23|9"); 
  • 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

×
×
  • Create New...