function onAfterWrite()
 {
     parent::onAfterWrite();
     $mainGroup = MerchantGroupDOD::get_main_group();
     if ($mainGroup && $this->MerchantGroupCode()) {
         if ($this->CanEditType != 'OnlyTheseUsers') {
             $this->writeToStage('Stage');
             $this->doPublish();
         }
     }
 }
 function onAfterWrite()
 {
     parent::onAfterWrite();
     $parent = DataObject::get_by_id("MerchantPage", $this->ParentID);
     $filter = '';
     if ($parent) {
         $products = DataObject::get('MerchantProduct', "\"ParentID\" = {$this->ParentID}");
         if ($products) {
             $products = implode(',', $products->map('ID', 'ID'));
             $filter = " AND \"ProductID\" NOT IN ({$products})";
         }
     }
     DB::query("DELETE FROM \"Product_ProductGroups\" WHERE \"ProductGroupID\" = {$this->ID}{$filter}");
 }