src/Contao/View/Contao2BackendView/ActionHandler/DeleteHandler.php
protected function deepDelete(EnvironmentInterface $environment, ModelIdInterface $modelId)
    {
        /** @var DefaultModelRelationshipDefinition $relationships */
        $relationships = $environment->getDataDefinition()->getDefinition('model-relationships');

        $childConditions = $relationships->getChildConditions($modelId->getDataProviderName());

        // delete child element before delete parent element
        /** @var ParentChildConditionInterface $childCondition */
        foreach ($childConditions as $childCondition) {
            $destinationChildConditions = $relationships->getChildConditions($childCondition->getDestinationName());
            if (empty($destinationChildConditions)) {
                continue;
            }

            $dataProvider                 = $environment->getDataProvider($modelId->getDataProviderName());
            $model                        = $dataProvider->fetch(
                $dataProvider->getEmptyConfig()->setId($modelId->getId())
            );
            $destinationChildDataProvider = $environment->getDataProvider($childCondition->getDestinationName());

            /** @var DefaultCollection $destinationChildModels */
            $destinationChildModels = $destinationChildDataProvider->fetchAll(
                $dataProvider->getEmptyConfig()->setFilter($childCondition->getFilter($model))
            );
            if ($destinationChildModels->count() < 1) {
                continue;
            }

            foreach ($destinationChildModels as $destinationChildModel) {
                $this->deepDelete($environment, ModelId::fromModel($destinationChildModel));
            }
        }

        foreach ($childConditions as $childCondition) {
            $dataProvider      = $environment->getDataProvider($modelId->getDataProviderName());
            $model             = $dataProvider->fetch($dataProvider->getEmptyConfig()->setId($modelId->getId()));
            $childDataProvider = $environment->getDataProvider($childCondition->getDestinationName());

            $filters = $childCondition->getFilter($model);
            /** @var DefaultCollection $childModels */
            $childModels = $childDataProvider->fetchAll($dataProvider->getEmptyConfig()->setFilter($filters));
            if ($childModels->count() < 1) {
                continue;
            }

            foreach ($childModels as $childModel) {
                // Trigger event before the model will be deleted.
                $preDeleteEvent = new PreDeleteModelEvent($environment, $childModel);
                $environment->getEventDispatcher()->dispatch($preDeleteEvent::NAME, $preDeleteEvent);

                $childDataProvider->delete($childModel);

                // Trigger event after the model is deleted.
                $postDeleteEvent = new PostDeleteModelEvent($environment, $childModel);
                $environment->getEventDispatcher()->dispatch($postDeleteEvent::NAME, $postDeleteEvent);
            }
        }
    }
protected function deepDelete(EnvironmentInterface $environment, ModelIdInterface $modelId)
    {
        /** @var DefaultModelRelationshipDefinition $relationships */
        $relationships = $environment->getDataDefinition()->getDefinition('model-relationships');

        $childConditions = $relationships->getChildConditions($modelId->getDataProviderName());

        // delete child element before delete parent element
        /** @var ParentChildConditionInterface $childCondition */
        foreach ($childConditions as $childCondition) {
            $destinationChildConditions = $relationships->getChildConditions($childCondition->getDestinationName());
            if (empty($destinationChildConditions)) {
                continue;
            }

            $dataProvider                 = $environment->getDataProvider($modelId->getDataProviderName());
            $model                        = $dataProvider->fetch(
                $dataProvider->getEmptyConfig()->setId($modelId->getId())
            );
            $destinationChildDataProvider = $environment->getDataProvider($childCondition->getDestinationName());

            /** @var DefaultCollection $destinationChildModels */
            $destinationChildModels = $destinationChildDataProvider->fetchAll(
                $dataProvider->getEmptyConfig()->setFilter($childCondition->getFilter($model))
            );
            if ($destinationChildModels->count() < 1) {
                continue;
            }

            foreach ($destinationChildModels as $destinationChildModel) {
                $this->deepDelete($environment, ModelId::fromModel($destinationChildModel));
            }
        }

        foreach ($childConditions as $childCondition) {
            $dataProvider      = $environment->getDataProvider($modelId->getDataProviderName());
            $model             = $dataProvider->fetch($dataProvider->getEmptyConfig()->setId($modelId->getId()));
            $childDataProvider = $environment->getDataProvider($childCondition->getDestinationName());

            $filters = $childCondition->getFilter($model);
            /** @var DefaultCollection $childModels */
            $childModels = $childDataProvider->fetchAll($dataProvider->getEmptyConfig()->setFilter($filters));
            if ($childModels->count() < 1) {
                continue;
            }

            foreach ($childModels as $childModel) {
                // Trigger event before the model will be deleted.
                $preDeleteEvent = new PreDeleteModelEvent($environment, $childModel);
                $environment->getEventDispatcher()->dispatch($preDeleteEvent::NAME, $preDeleteEvent);

                $childDataProvider->delete($childModel);

                // Trigger event after the model is deleted.
                $postDeleteEvent = new PostDeleteModelEvent($environment, $childModel);
                $environment->getEventDispatcher()->dispatch($postDeleteEvent::NAME, $postDeleteEvent);
            }
        }
    }
protected function deepDelete ( EnvironmentInterface $ environment , ModelIdInterface $ modelId ) { /** @var DefaultModelRelationshipDefinition $relationships */ $ relationships = $ environment -> getDataDefinition ( ) -> getDefinition ( 'model-relationships' ) ; $ childConditions = $ relationships -> getChildConditions ( $ modelId -> getDataProviderName ( ) ) ; // delete child element before delete parent element /** @var ParentChildConditionInterface $childCondition */ foreach ( $ childConditions as $ childCondition ) { $ destinationChildConditions = $ relationships -> getChildConditions ( $ childCondition -> getDestinationName ( ) ) ; if ( empty ( $ destinationChildConditions ) ) { continue ; } $ dataProvider = $ environment -> getDataProvider ( $ modelId -> getDataProviderName ( ) ) ; $ model = $ dataProvider -> fetch ( $ dataProvider -> getEmptyConfig ( ) -> setId ( $ modelId -> getId ( ) ) ) ; $ destinationChildDataProvider = $ environment -> getDataProvider ( $ childCondition -> getDestinationName ( ) ) ; /** @var DefaultCollection $destinationChildModels */ $ destinationChildModels = $ destinationChildDataProvider -> fetchAll ( $ dataProvider -> getEmptyConfig ( ) -> setFilter ( $ childCondition -> getFilter ( $ model ) ) ) ; if ( $ destinationChildModels -> count ( ) < 1 ) { continue ; } foreach ( $ destinationChildModels as $ destinationChildModel ) { $ this -> deepDelete ( $ environment , ModelId :: fromModel ( $ destinationChildModel ) ) ; } } foreach ( $ childConditions as $ childCondition ) { $ dataProvider = $ environment -> getDataProvider ( $ modelId -> getDataProviderName ( ) ) ; $ model = $ dataProvider -> fetch ( $ dataProvider -> getEmptyConfig ( ) -> setId ( $ modelId -> getId ( ) ) ) ; $ childDataProvider = $ environment -> getDataProvider ( $ childCondition -> getDestinationName ( ) ) ; $ filters = $ childCondition -> getFilter ( $ model ) ; /** @var DefaultCollection $childModels */ $ childModels = $ childDataProvider -> fetchAll ( $ dataProvider -> getEmptyConfig ( ) -> setFilter ( $ filters ) ) ; if ( $ childModels -> count ( ) < 1 ) { continue ; } foreach ( $ childModels as $ childModel ) { // Trigger event before the model will be deleted. $ preDeleteEvent = new PreDeleteModelEvent ( $ environment , $ childModel ) ; $ environment -> getEventDispatcher ( ) -> dispatch ( $ preDeleteEvent :: NAME , $ preDeleteEvent ) ; $ childDataProvider -> delete ( $ childModel ) ; // Trigger event after the model is deleted. $ postDeleteEvent = new PostDeleteModelEvent ( $ environment , $ childModel ) ; $ environment -> getEventDispatcher ( ) -> dispatch ( $ postDeleteEvent :: NAME , $ postDeleteEvent ) ; } } }
Delete all deep models.

@param EnvironmentInterface $environment Environment.
@param ModelIdInterface     $modelId     The Model Id.

@return void

@SuppressWarnings(PHPMD.LongVariableName)
Delete all deep models .