Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

DataList filter using OR


Go to End


8 Posts   6626 Views

Avatar
Morgan

Community Member, 5 Posts

14 November 2012 at 8:12am

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?

Avatar
Morgan

Community Member, 5 Posts

19 November 2012 at 12:04am

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?

Avatar
martimiz

Forum Moderator, 1391 Posts

19 November 2012 at 1:34am

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

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

Avatar
Morgan

Community Member, 5 Posts

19 November 2012 at 2:44am

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

Avatar
martimiz

Forum Moderator, 1391 Posts

19 November 2012 at 4:09am

Edited: 19/11/2012 4:10am

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 :)

Avatar
kinglozzer

Community Member, 187 Posts

20 November 2012 at 10:07pm

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

Avatar
Morgan

Community Member, 5 Posts

20 November 2012 at 11:42pm

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.

Avatar
m-phil

Community Member, 37 Posts

2 April 2014 at 8:19pm

Edited: 02/04/2014 8:30pm

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')