public function onAfterPublish()
    {
        // store IDs of fields we've published
        $seenIDs = [];
        foreach ($this->owner->Fields() as $field) {
            // store any IDs of fields we publish so we don't unpublish them
            $seenIDs[] = $field->ID;
            $field->doPublish(Versioned::DRAFT, Versioned::LIVE);
            $field->destroy();
        }

        // fetch any orphaned live records
        $live = Versioned::get_by_stage(EditableFormField::class, Versioned::LIVE)
            ->filter([
                'ParentID' => $this->owner->ID,
            ]);

        if (!empty($seenIDs)) {
            $live = $live->exclude([
                'ID' => $seenIDs,
            ]);
        }

        // delete orphaned records
        foreach ($live as $field) {
            $field->deleteFromStage(Versioned::LIVE);
            $field->destroy();
        }
    }
public function onAfterPublish()
    {
        // store IDs of fields we've published
        $seenIDs = [];
        foreach ($this->owner->Fields() as $field) {
            // store any IDs of fields we publish so we don't unpublish them
            $seenIDs[] = $field->ID;
            $field->doPublish(Versioned::DRAFT, Versioned::LIVE);
            $field->destroy();
        }

        // fetch any orphaned live records
        $live = Versioned::get_by_stage(EditableFormField::class, Versioned::LIVE)
            ->filter([
                'ParentID' => $this->owner->ID,
            ]);

        if (!empty($seenIDs)) {
            $live = $live->exclude([
                'ID' => $seenIDs,
            ]);
        }

        // delete orphaned records
        foreach ($live as $field) {
            $field->deleteFromStage(Versioned::LIVE);
            $field->destroy();
        }
    }
public function onAfterPublish ( ) { // store IDs of fields we've published $ seenIDs = [ ] ; foreach ( $ this -> owner -> Fields ( ) as $ field ) { // store any IDs of fields we publish so we don't unpublish them $ seenIDs [ ] = $ field -> ID ; $ field -> doPublish ( Versioned :: DRAFT , Versioned :: LIVE ) ; $ field -> destroy ( ) ; } // fetch any orphaned live records $ live = Versioned :: get_by_stage ( EditableFormField :: class , Versioned :: LIVE ) -> filter ( [ 'ParentID' => $ this -> owner -> ID , ] ) ; if ( ! empty ( $ seenIDs ) ) { $ live = $ live -> exclude ( [ 'ID' => $ seenIDs , ] ) ; } // delete orphaned records foreach ( $ live as $ field ) { $ field -> deleteFromStage ( Versioned :: LIVE ) ; $ field -> destroy ( ) ; } }
Remove any orphaned child records on publish
Remove any orphaned child records on publish