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

Excluding data from a dataobjectset


Go to End


3 Posts   1495 Views

Avatar
pingu

Community Member, 75 Posts

28 November 2010 at 2:56am

Is there a way to exclude a result from a dataobjectset without affecting it in the database?
I've tried remove() but this writes to the database.

My scenario is as follows:
- I have a list of articles
- One of those articles flagged as "featured" will be a featured article, this will be selected at random from all articles marked as featured
- I need to get the dataobjectset of the rest of the articles, excluding the featured one

Avatar
swaiba

Forum Moderator, 1899 Posts

28 November 2010 at 8:05am

remove does work, but how about...

$newresult = new DataObjectSet();
foreach ($result as $do) {
if (!$do->Featured)
$newresult->push($do);

or in the template
<% control result %>
<% if Featured != true %>
$Name
<% end_if %>
<% end_control %>

or even better use the where clause of the DataOBject::get()

Avatar
pingu

Community Member, 75 Posts

28 November 2010 at 8:48pm

Thanks swaiba, I ended up using where after I posted. I was trying to avoid code repetition, but in this instance it was the most efficient way to do it.