Jump to content

Bug: Repeater count=0 in selector not working


Soma
 Share

Recommended Posts

I can't get the pages that have repeater.count=0

No results:

$pa = $pages->find("template=basic-page,myrepeater.count=0");
 

doesn't give me the pages where there's no repeater element created yet.

Works:

$pa = $pages->find("template=basic-page,myrepeater.count>0");
 

this works correct

$pa = $pages->find("template=basic-page,myrepeater.count=2");
 

this also.

I have Language Support installed, in case that matter at all. I have tested in different installs and I can't get it to work, but thought I was able to do this count=0 on repeaters the other day.

Anybody able to reproduce this?

Link to comment
Share on other sites

Testing further:

Deinstalled Language Support to rule that out.

Now I found out:

The repeater.count=0 only works for pages that are once saved after the repeater is added to the template. So to say it different: For pages that are not yet saved, once after the repeater was added to the template, it doesn't find them.

  • Like 1
Link to comment
Share on other sites

Yep, seems to work like you described Soma here as well.

This is what I'm seeing: when a repeater field "xx" is added to a template "yy", no data is written into field_xx for pages with template "yy". And because selector "zz.count" relies on column "count" in field_xx, the selector doesn't work. But when such a page is saved the next time, a row with proper "count" value is written into field_xx and the selector works as expected.

I guess those rows should be written for all pre-existing pages of the template when a repeater is added. Either that or some more SQL to cover the non-existing rows when <repeater>.count is used.

  • Like 1
Link to comment
Share on other sites

Thanks Nik fr confirming.

Another thing: (was my problem only)

Further more I noticed something else again maybe related.

I created repeater items via API for those pages.

Now I can't access them with $page->repeater->first()

$page->repeater->first() // doesn't return the first repeater although it's there.

Once I go to edit the page and change a value on that repeater item and save, it works.

Ok this is my fault sorry. I was using the Shop module and the integer value is required to show the add to cart and I missed that it requires the price to show.

Link to comment
Share on other sites

Thanks for the report. I was able to duplicate it too. Can you try replacing your /wire/modules/Fieldtype/FieldtypeRepeater/FieldtypeRepeater.module with the attached file and let me know if that fixes it for you too?

FieldtypeRepeater.module

To fix it, I replaced this line get getMatchQuery():

$query->where("($table.count{$operator}$value)");

with this:

if(($operator == '=' && $value == 0) || 
   (in_array($operator, array('<', '<=')) && $value > -1) ||
   ($operator == '!=' && $value)) {
        
    $templateIDs = array();
    foreach($field->getTemplates() as $template) $templateIDs[] = (int) $template->id; 
    if(count($templateIDs)) {
        $templateIDs = implode(',', $templateIDs); 
        $sql =  "($table.count{$operator}$value OR " . 
                "($table.count IS NULL AND pages.templates_id IN($templateIDs)))";
        $query->where($sql); 
    } else {
        $query->where("1>2"); // forced non-match
    }

} else {
    $query->where("($table.count{$operator}$value)"); 
}

Hopefully I got the logic right on this one, as it's a little different need than count() situations for other Fieldtypes. Most fieldtypes don't keep track of their own counts and PW relies on the quantity of rows present in the table instead. 

Link to comment
Share on other sites

  • 6 months later...

I see that Ryan's fix above has been downloaded 19 times before I just did, but no-one has commented on its efficacy. It doesn't seem to have worked for me, as I still have the problem Soma originally reported, except that saving the pages makes no difference.

Does anyone have any comments or updates?

Link to comment
Share on other sites

  • 5 months later...

I ran into this issue again, and ryans file works, but only for new entries (after fix is applied).

I've got a project with lot of repeater data that I can't enter all over again. Is there a way to loop through all repeaters and publish them for all languages?

Link to comment
Share on other sites

I tried looping through the repeater values and publishing them for all languages, but no luck so far:

(site is 2.4)

$pages->setOutputFormatting(false);
$pps = $pages->find('has_parent=/processwire/repeaters/for-field-116/');
foreach($pps as $p) {
   foreach($languages as $lang) {
     if($lang->isDefault()) continue;
     $p->set("status$lang", 1);
     echo "set status$lang" . $p->title . ' <br>';
     $p->save();
   }
} 
die;
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...