Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Excluding data from a dataobjectset

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

Page: 1
Go to End
Author Topic: 770 Views
  • pingu
    Avatar
    Community Member
    75 Posts

    Excluding data from a dataobjectset Link to this post

    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

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Excluding data from a dataobjectset Link to this post

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

  • pingu
    Avatar
    Community Member
    75 Posts

    Re: Excluding data from a dataobjectset Link to this post

    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.

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