Jump to content

Selector. Where now between start date and end date or now bigger than start date and end date is empty


Elchin
 Share

Recommended Posts

Hi.

I want select pages where now between date and end_date or now bigger than date and end_date is empty.

I have five tried variants:

$start = strtotime(date('Y-m-d') . " 00:00:00");
$results = $page->children("foo=(date<$start,date_end=''),bar=(date<$start,date_end>=$start),sort=-date,limit=12");

$start = strtotime(date('Y-m-d') . " 00:00:00");
$results = $page->children("date<$start,(date_end='',date_end>=$start),sort=-date,limit=12");

$start = strtotime(date('Y-m-d') . " 00:00:00");
$results = $page->children("date_end=''|date_end>=$start,date<$start,sort=-date,limit=12");

$start = strtotime(date('Y-m-d') . " 00:00:00");
$results = $page->children("!date_end|date_end>=$start,date<$start,sort=-date,limit=12");

$start = strtotime(date('Y-m-d') . " 00:00:00");
$results = $page->children("date_end>=$start|!date_end,date<$start,sort=-date,limit=12");

All this variants not worked for me and returned zero results.

Link to comment
Share on other sites

ok, tested a little and the selectors seem to work for me, primarily tested with this one:

$results = $page->children("!date_end|date_end>=$start,date<$start,sort=-date,limit=12");

Did you try outputting your date and date_end fields unformatted to see if they contain proper values?

  • Like 1
Link to comment
Share on other sites

Hi @Elchin

I got them in the past but the query was to slow so I finally used a custom query to achieve that, you might be interested as I don't know the number of pages your trying to fetch - I give you a small module which could be extended, shipped with a hook function which return all pages between two dates given a fieldname and a template id :

 

  Reveal hidden contents

 

 

 

 

Edited by flydev
code typo
  • Like 4
Link to comment
Share on other sites

  On 3/11/2019 at 6:09 PM, flydev said:

$templateId = $this->sanitizer->datetime($event->arguments(3));

Expand  

Should this have been $this->sanitizer->int() ?

It would be awesome to be also able to pass a selector string to the method (e.g. "some_field%=value") but I guess it would get quite involved to translate that into SQL.

Link to comment
Share on other sites

  On 3/11/2019 at 5:39 PM, Autofahrn said:

ok, tested a little and the selectors seem to work for me, primarily tested with this one:

$results = $page->children("!date_end|date_end>=$start,date<$start,sort=-date,limit=12");

Did you try outputting your date and date_end fields unformatted to see if they contain proper values?

Expand  

This not work for me.

The example below returns pages that have property `date_end` and `date_end`>=$start

$results = $page->children("date_end>=$start,date<$start,sort=-date,limit=12");

The example below returns pages that not have property `date_end` at all

$results = $page->children("!date_end,date<$start,sort=-date,limit=12");

But I can't get this pages with this two conditions together.

I don't know why but the OR condition `!date_end|date_end>=$start` didn't worked in this case.

  On 3/11/2019 at 6:09 PM, flydev said:

Hi @Elchin

I got them in the past but the query was to slow so I finally used a custom query to achieve that, you might be interested as I don't know the number of pages your trying to fetch - I give you a small module which could be extended, shipped with a hook function which return all pages between two dates given a fieldname and a template id :

 

  Reveal hidden contents

 

 

 

 

Expand  

How can I use paging in this case?

Link to comment
Share on other sites

  On 3/12/2019 at 7:17 AM, Elchin said:

Please explain why my variants not works and why your variant works?

Expand  

 

  On 3/11/2019 at 2:24 PM, Elchin said:

$results = $page->children("foo=(date<$start,date_end=''),bar=(date<$start,date_end>=$start),sort=-date,limit=12");

Expand  

The naming of the OR-groups is not correct. You only name OR-groups if there is more than one set of OR-groups and one group in each named set must match. Your selector here is saying that both (date<$start,date_end='') and (date<$start,date_end>=$start) must match which is impossible because these contradict each other.

  On 3/11/2019 at 2:24 PM, Elchin said:

results = $page->children("date<$start,(date_end='',date_end>=$start),sort=-date,limit=12");

Expand  

Wrong use of OR-group syntax with only one OR condition.

  On 3/11/2019 at 2:24 PM, Elchin said:

$results = $page->children("date_end=''|date_end>=$start,date<$start,sort=-date,limit=12");

Expand  
  On 3/11/2019 at 2:24 PM, Elchin said:

$results = $page->children("!date_end|date_end>=$start,date<$start,sort=-date,limit=12");

Expand  
  On 3/11/2019 at 2:24 PM, Elchin said:

$results = $page->children("date_end>=$start|!date_end,date<$start,sort=-date,limit=12");

Expand  

Wrong use of pipe character. The pipe character can only go between field names in a selector clause...

field_a|field_b~=foo

...or between values in a selector clause...

field_a%=foo|bar

 

  • Like 5
Link to comment
Share on other sites

  On 3/12/2019 at 6:03 AM, Elchin said:

How can I use paging in this case?

Expand  

 

To paginate the result, you have to know the total amount of pages you fetch so we must add another hook to our module (full code here) :

 

  Reveal hidden contents

 

 

Then you just have to write a little script to paginate the result. To try it, create on you webroot directory a file called `test.php` and put the following code in then navigate to hxxp://example.com/test.php :

 

  Reveal hidden contents

 

Result :

pagination.gif

 

 

  On 3/12/2019 at 5:51 AM, Robin S said:

Should this have been $this->sanitizer->int() ?

Expand  

correct, corrected - a bad copy pasta moment ?

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...