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.

General Questions

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

How to subtract DataObjects


Reply

4 Posts   477 Views

Avatar
woodb2

10 July 2012 at 7:58am Community Member, 48 Posts

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

Avatar
swaiba

10 July 2012 at 8:44pm (Last edited: 10 July 2012 8:45pm), Forum Moderator, 1796 Posts

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

Avatar
dpde

10 July 2012 at 11:28pm Community Member, 14 Posts

http://api.silverstripe.org/2.4/sapphire/model/DataObjectSet.html#methodremove

Avatar
woodb2

10 July 2012 at 11:31pm Community Member, 48 Posts

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