Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Filter LessThanOrEqual and GreaterThanOrEqual don't work (SS 3.1.2)

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 321 Views
  • Stephan
    Avatar
    Community Member
    55 Posts

    Filter LessThanOrEqual and GreaterThanOrEqual don't work (SS 3.1.2) Link to this post

    Hi experts,
    I have created a news module to show articles on my website.
    I want to show only news which are between my start- and end-date.
    So I created:

    $news = NewsArticle::get()->filter(array(
       'StartDate:LessThanOrEqual' => date('Y-m-d'),
       'EndDate:GreaterThanOrEqual' => date('Y-m-d')
       ))->sort(array('Date' =>'DESC'));


    which doesn't work.
    Silverstripe debug says:

    [User Error] Uncaught InvalidArgumentException: ExactMatchFilter does not accept LessThanOrEqual as modifiers
    GET /index.php/de/aktuelles/?showqueries=1
    Line 20 in /Applications/XAMPP/xamppfiles/htdocs/framework/search/filters/ExactMatchFilter.php
    Why is Silverstripe using the ExactMatchFilter ?

    However

    $news = NewsArticle::get()->filter(array(
       'StartDate:LessThan' => date('Y-m-d'),
       'EndDate:GreaterThan' => date('Y-m-d')
       ))->sort(array('Date' =>'DESC'));


    works fine!
    But doesn't give me the articles I do need.

    Running SS 3.1.2

    TIA Stephan

  • simon_w
    Avatar
    Forum Moderator
    467 Posts

    Re: Filter LessThanOrEqual and GreaterThanOrEqual don't work (SS 3.1.2) Link to this post

    These were added to 3.1 after 3.1.2 was released, so they'll be in 3.1.3. Until then, you can do the opposite of what you're after to get the same result.

    That is, filter('Field:LessThanOrEqual', value) becomes exclude('Field:GreaterThan' value). Likewise for GreaterThanOrEqual.

  • Stephan
    Avatar
    Community Member
    55 Posts

    Re: Filter LessThanOrEqual and GreaterThanOrEqual don't work (SS 3.1.2) Link to this post

    Ok, thanks for the info.

    So tried:

    $news = NewsArticle::get()->exclude(array(
       'StartDate:GreaterThan' => date('Y-m-d'),
       'EndDate:LessThan' => date('Y-m-d')
       ))->sort(array('Date' =>'DESC'));

    But SilverStripe is fooling me,
    instead of making

    WHERE NOT(("NewsArticle_Live"."StartDate" > '2014-01-13') OR ("NewsArticle_Live"."EndDate" < '2014-01-13'))

    the following SQL Query is generated:

    WHERE (("NewsArticle_Live"."StartDate" <= '2014-01-13') OR ("NewsArticle_Live"."EndDate" >= '2014-01-13'))

    this is wrong.

    So I changed my code to

    $news = NewsArticle::get()->filter(array(
       'StartDate:GreaterThan:Not' => date('Y-m-d'),
       'EndDate:LessThan:Not' => date('Y-m-d')
       ))->sort(array('Date' =>'DESC'));

    which makes the following nice SQL Query:

    ("NewsArticle_Live"."StartDate" <= '2014-01-13') AND ("NewsArticle_Live"."EndDate" >= '2014-01-13')

    321 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.