public function updateByConfig(
        SectionConfig $sectionConfig,
        SectionInterface $section,
        bool $history = false
    ): SectionInterface {

        if ($history) {
            /** @var SectionInterface $sectionHistory */
            $sectionHistory = $this->copySectionDataToSectionHistoryEntity($section); // 1
            $sectionHistory->setVersioned(new \DateTime()); // 2
            $this->sectionHistoryManager->create($sectionHistory); // 2
            $version = $this->getHighestVersion($section->getHandle());
            $section->setVersion(1 + $version->toInt()); // 3
        }

        $section->removeFields(); // 4

        $fields = $this->fieldManager->readByHandles($sectionConfig->getFields()); // 5

        foreach ($fields as $field) {
            $section->addField($field);
        } // 6

        $section->setName((string) $sectionConfig->getName()); // 7
        $section->setHandle((string) $sectionConfig->getHandle()); // 7
        $section->setConfig($sectionConfig->toArray()); // 8

        $this->entityManager->persist($section); // 9
        $this->entityManager->flush(); // 9

        return $section;
    }
public function updateByConfig(
        SectionConfig $sectionConfig,
        SectionInterface $section,
        bool $history = false
    ): SectionInterface {

        if ($history) {
            /** @var SectionInterface $sectionHistory */
            $sectionHistory = $this->copySectionDataToSectionHistoryEntity($section); // 1
            $sectionHistory->setVersioned(new \DateTime()); // 2
            $this->sectionHistoryManager->create($sectionHistory); // 2
            $version = $this->getHighestVersion($section->getHandle());
            $section->setVersion(1 + $version->toInt()); // 3
        }

        $section->removeFields(); // 4

        $fields = $this->fieldManager->readByHandles($sectionConfig->getFields()); // 5

        foreach ($fields as $field) {
            $section->addField($field);
        } // 6

        $section->setName((string) $sectionConfig->getName()); // 7
        $section->setHandle((string) $sectionConfig->getHandle()); // 7
        $section->setConfig($sectionConfig->toArray()); // 8

        $this->entityManager->persist($section); // 9
        $this->entityManager->flush(); // 9

        return $section;
    }
public function updateByConfig ( SectionConfig $ sectionConfig , SectionInterface $ section , bool $ history = false ) : SectionInterface { if ( $ history ) { /** @var SectionInterface $sectionHistory */ $ sectionHistory = $ this -> copySectionDataToSectionHistoryEntity ( $ section ) ; // 1 $ sectionHistory -> setVersioned ( new \ DateTime ( ) ) ; // 2 $ this -> sectionHistoryManager -> create ( $ sectionHistory ) ; // 2 $ version = $ this -> getHighestVersion ( $ section -> getHandle ( ) ) ; $ section -> setVersion ( 1 + $ version -> toInt ( ) ) ; // 3 } $ section -> removeFields ( ) ; // 4 $ fields = $ this -> fieldManager -> readByHandles ( $ sectionConfig -> getFields ( ) ) ; // 5 foreach ( $ fields as $ field ) { $ section -> addField ( $ field ) ; } // 6 $ section -> setName ( ( string ) $ sectionConfig -> getName ( ) ) ; // 7 $ section -> setHandle ( ( string ) $ sectionConfig -> getHandle ( ) ) ; // 7 $ section -> setConfig ( $ sectionConfig -> toArray ( ) ) ; // 8 $ this -> entityManager -> persist ( $ section ) ; // 9 $ this -> entityManager -> flush ( ) ; // 9 return $ section ; }
A section config is stored in the section history before it's updated.

1. Copy the data from the Section entity to the SectionHistory entity
2. Persist it in the entity history
3. Bump the version (+1)
4. Clear the field many to many relationships
5. Fetch the fields based on the config
6. Add them to the Section entity
7. Set the fields with the config values
8. Set the config
9. Persist the entity

This new section might be created from an updated config yml. It's recommended to copy the old
config yml before you update it. That way the config yml's history will be in compliance with the
config stored in the database.

This will only update the config stored in the database. The generators will have to be called
to make the version change complete.

@param SectionConfig $sectionConfig
@param SectionInterface $section
@param bool $history

@return SectionInterface
A section config is stored in the section history before it s updated .