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.

General Questions /

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

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

How to subtract DataObjects


Go to End


4 Posts   1526 Views

Avatar
woodb2

Community Member, 48 Posts

10 July 2012 at 7:58am

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

Forum Moderator, 1899 Posts

10 July 2012 at 8:44pm

Edited: 10/07/2012 8:45pm

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
woodb2

Community Member, 48 Posts

10 July 2012 at 11:31pm

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