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   481 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, 1798 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
dpde

Community Member, 14 Posts

10 July 2012 at 11:28pm

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

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