function onAfterWrite() { parent::onAfterWrite(); $parent = DataObject::get_by_id("MerchantPage", $this->ParentID); $filter = ''; if ($parent) { $locations = DataObject::get('MerchantLocation', "ParentID = {$this->ParentID}"); if ($locations) { $locations = implode(',', $locations->map('ID', 'ID')); $filter = " AND \"ProductGroupID\" NOT IN ({$locations})"; } } DB::query("DELETE FROM \"Product_ProductGroups\" WHERE \"ProductID\" = {$this->ID} {$filter}"); }