Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » DataList filter using OR

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

Page: 1
Go to End
Author Topic: 1003 Views
  • Morgan
    Avatar
    Community Member
    5 Posts

    DataList filter using OR Link to this post

    I need assistance on how to filter a datalist by where Firstname = 'Sam' or surname = 'paul'. How do you achieve this using the new ORM?

  • Morgan
    Avatar
    Community Member
    5 Posts

    Re: DataList filter using OR Link to this post

    Hi All,
    To elaborate on my issue

    I have a class called students with two fields : Firstname and Surname. I want A datalist of students with either Firstname Samuel or Surname 'Paul'. How do you achieve this with the new ORM?

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: DataList filter using OR Link to this post

    Hi,
    I think you can still use where() as in:

    $students = Student::get()->where("Firstname = 'Samuel' OR Surname = 'Samuel'");

  • Morgan
    Avatar
    Community Member
    5 Posts

    Re: DataList filter using OR Link to this post

    Thanks martimiz,
    It seems like filter method cannot be used to achieve this.

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: DataList filter using OR Link to this post

    Maybe you could even:

    $students = Student::get()->exclude(array('Firstname:not' => 'Samuel', 'Surname:not' => 'Paul'));

    That would be the other way around, wouldn't it? But I've not tested this and also it feels a bit strange and unreadable...

    So maybe we just missed something

  • kinglozzer
    Avatar
    Community Member
    139 Posts

    Re: DataList filter using OR Link to this post

    $students = Student::get()->filter('Firstname', array('Samuel', 'Paul'));

  • Morgan
    Avatar
    Community Member
    5 Posts

    Re: DataList filter using OR Link to this post

    kinglozzer
    I think yours is for a result set for where firstname is either paul or samuel. My question was for where firstname is samuel or surname is paul. I implemented martimiz first ansewer and it was okay.

  • m-phil
    Avatar
    Community Member
    37 Posts

    Re: DataList filter using OR Link to this post

    Student::get()->filterAny(array('Firstname' => 'Samuel', 'Firstname' => 'Paul'));
    // SQL: WHERE "Firstname" = 'Samuel' OR "Firstname" = 'Paul'

    Student::get()->filterAny(array('Firstname' => array('Samuel', 'Paul')));
    // SQL: WHERE "Firstname" IN ('Samuel', 'Paul')

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