Jump to:

23355 Posts in 17838 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » How to subtract DataObjects

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 434 Views
  • woodb2
    Avatar
    Community Member
    48 Posts

    How to subtract DataObjects Link to this post

    I've found I can merge dataobjects just fine, but I need to subtract Dataobjects too. It says the method subtract does not exist.

    Is there a way to subtract $alreadypicked ?

    $enhancements = new DataObjectSet();
       $alreadypicked = DataObject::get('Enhancement', 'ProjectsID != 0');
       $enhancements->merge(DataObject::get('Enhancement', 'StatusLevelsID = 8')); // In Progress
       $enhancements->subtract($alreadypicked);
       $enhancements->merge(DataObject::get('Enhancement', "ProjectsID = {$this->ID}"));

    Thanks,
    Brian

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: How to subtract DataObjects Link to this post

    how about...

    $enhancements = new DataObjectSet();
    $alreadypicked = DataObject::get('Enhancement', 'ProjectsID != 0');
    $enhancements->merge(DataObject::get('Enhancement', 'StatusLevelsID = 8')); // In Progress
    if ($alreadypicked) foreach ($alreadypicked as $item) {
    $enhancements->remove($item);
    }

    but having looked again - what you are doing is crazy... why not just use a bit more SQL...

    $enhancements = DataObject::get('Enhancement', 'StatusLevelsID = 8 AND ProjectsID != 0');

  • dpde
    Avatar
    Community Member
    14 Posts
  • woodb2
    Avatar
    Community Member
    48 Posts

    Re: How to subtract DataObjects Link to this post

    Thanks!!! Your second option was what I was originally looking for. I entered it incorrectly as:

    $enhancements->merge(DataObject::get('Enhancement', 'StatusLevelsID = 8' AND 'ProjectsID != 0'))

    Which gave me all StatusLevelsID of 8 and all ProjectsID of 0.

    I was also wanting to know how to subtract (remove) an item too, so thanks again.

    Brian

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