src/Sulu/Component/Doctrine/ReferencesOption.php
public function postGenerateSchemaTable(GenerateSchemaTableEventArgs $args)
    {
        $classMetadata = $args->getClassMetadata();
        $table = $args->getClassTable();

        foreach ($classMetadata->getFieldNames() as $fieldName) {
            $mapping = $classMetadata->getFieldMapping($fieldName);

            if (!isset($mapping['options']['references'])) {
                continue;
            }

            $referencesOptions = $mapping['options']['references'];

            $unknownOptions = array_diff_key($referencesOptions, array_flip(self::$knownOptions));

            if (count($unknownOptions) > 0) {
                throw new RuntimeException(
                    sprintf(
                        'Unknown options "%s" in the "references" option in the Doctrine schema of %s::%s.',
                        implode('", "', array_keys($unknownOptions)),
                        $classMetadata->getReflectionClass()->getName(),
                        $fieldName
                    )
                );
            }

            if (!isset($referencesOptions['entity'])) {
                throw new RuntimeException(
                    sprintf(
                        'Missing option "entity" in the "references" option in the Doctrine schema of %s::%s.',
                        $classMetadata->getReflectionClass()->getName(),
                        $fieldName
                    )
                );
            }

            if (!isset($referencesOptions['field'])) {
                throw new RuntimeException(
                    sprintf(
                        'Missing option "field" in the "references" option in the Doctrine schema of %s::%s.',
                        $classMetadata->getReflectionClass()->getName(),
                        $fieldName
                    )
                );
            }

            $localColumnName = $classMetadata->getColumnName($fieldName);

            /** @var ClassMetadata $foreignClassMetadata */
            $foreignClassMetadata = $this->managerRegistry->getManagerForClass($referencesOptions['entity'])
                ->getClassMetadata($referencesOptions['entity']);

            $foreignTable = $foreignClassMetadata->getTableName();
            $foreignColumnName = $foreignClassMetadata->getColumnName($referencesOptions['field']);
            $options = [];

            if (isset($referencesOptions['onDelete'])) {
                $options['onDelete'] = $referencesOptions['onDelete'];
            }

            if (isset($referencesOptions['onUpdate'])) {
                $options['onUpdate'] = $referencesOptions['onUpdate'];
            }

            $table->addForeignKeyConstraint(
                $foreignTable,
                [$localColumnName],
                [$foreignColumnName],
                $options
            );
        }
    }
public function postGenerateSchemaTable(GenerateSchemaTableEventArgs $args)
    {
        $classMetadata = $args->getClassMetadata();
        $table = $args->getClassTable();

        foreach ($classMetadata->getFieldNames() as $fieldName) {
            $mapping = $classMetadata->getFieldMapping($fieldName);

            if (!isset($mapping['options']['references'])) {
                continue;
            }

            $referencesOptions = $mapping['options']['references'];

            $unknownOptions = array_diff_key($referencesOptions, array_flip(self::$knownOptions));

            if (count($unknownOptions) > 0) {
                throw new RuntimeException(
                    sprintf(
                        'Unknown options "%s" in the "references" option in the Doctrine schema of %s::%s.',
                        implode('", "', array_keys($unknownOptions)),
                        $classMetadata->getReflectionClass()->getName(),
                        $fieldName
                    )
                );
            }

            if (!isset($referencesOptions['entity'])) {
                throw new RuntimeException(
                    sprintf(
                        'Missing option "entity" in the "references" option in the Doctrine schema of %s::%s.',
                        $classMetadata->getReflectionClass()->getName(),
                        $fieldName
                    )
                );
            }

            if (!isset($referencesOptions['field'])) {
                throw new RuntimeException(
                    sprintf(
                        'Missing option "field" in the "references" option in the Doctrine schema of %s::%s.',
                        $classMetadata->getReflectionClass()->getName(),
                        $fieldName
                    )
                );
            }

            $localColumnName = $classMetadata->getColumnName($fieldName);

            /** @var ClassMetadata $foreignClassMetadata */
            $foreignClassMetadata = $this->managerRegistry->getManagerForClass($referencesOptions['entity'])
                ->getClassMetadata($referencesOptions['entity']);

            $foreignTable = $foreignClassMetadata->getTableName();
            $foreignColumnName = $foreignClassMetadata->getColumnName($referencesOptions['field']);
            $options = [];

            if (isset($referencesOptions['onDelete'])) {
                $options['onDelete'] = $referencesOptions['onDelete'];
            }

            if (isset($referencesOptions['onUpdate'])) {
                $options['onUpdate'] = $referencesOptions['onUpdate'];
            }

            $table->addForeignKeyConstraint(
                $foreignTable,
                [$localColumnName],
                [$foreignColumnName],
                $options
            );
        }
    }
public function postGenerateSchemaTable ( GenerateSchemaTableEventArgs $ args ) { $ classMetadata = $ args -> getClassMetadata ( ) ; $ table = $ args -> getClassTable ( ) ; foreach ( $ classMetadata -> getFieldNames ( ) as $ fieldName ) { $ mapping = $ classMetadata -> getFieldMapping ( $ fieldName ) ; if ( ! isset ( $ mapping [ 'options' ] [ 'references' ] ) ) { continue ; } $ referencesOptions = $ mapping [ 'options' ] [ 'references' ] ; $ unknownOptions = array_diff_key ( $ referencesOptions , array_flip ( self :: $ knownOptions ) ) ; if ( count ( $ unknownOptions ) > 0 ) { throw new RuntimeException ( sprintf ( 'Unknown options "%s" in the "references" option in the Doctrine schema of %s::%s.' , implode ( '", "' , array_keys ( $ unknownOptions ) ) , $ classMetadata -> getReflectionClass ( ) -> getName ( ) , $ fieldName ) ) ; } if ( ! isset ( $ referencesOptions [ 'entity' ] ) ) { throw new RuntimeException ( sprintf ( 'Missing option "entity" in the "references" option in the Doctrine schema of %s::%s.' , $ classMetadata -> getReflectionClass ( ) -> getName ( ) , $ fieldName ) ) ; } if ( ! isset ( $ referencesOptions [ 'field' ] ) ) { throw new RuntimeException ( sprintf ( 'Missing option "field" in the "references" option in the Doctrine schema of %s::%s.' , $ classMetadata -> getReflectionClass ( ) -> getName ( ) , $ fieldName ) ) ; } $ localColumnName = $ classMetadata -> getColumnName ( $ fieldName ) ; /** @var ClassMetadata $foreignClassMetadata */ $ foreignClassMetadata = $ this -> managerRegistry -> getManagerForClass ( $ referencesOptions [ 'entity' ] ) -> getClassMetadata ( $ referencesOptions [ 'entity' ] ) ; $ foreignTable = $ foreignClassMetadata -> getTableName ( ) ; $ foreignColumnName = $ foreignClassMetadata -> getColumnName ( $ referencesOptions [ 'field' ] ) ; $ options = [ ] ; if ( isset ( $ referencesOptions [ 'onDelete' ] ) ) { $ options [ 'onDelete' ] = $ referencesOptions [ 'onDelete' ] ; } if ( isset ( $ referencesOptions [ 'onUpdate' ] ) ) { $ options [ 'onUpdate' ] = $ referencesOptions [ 'onUpdate' ] ; } $ table -> addForeignKeyConstraint ( $ foreignTable , [ $ localColumnName ] , [ $ foreignColumnName ] , $ options ) ; } }
Parses the mapping and adds foreign-key constraints for each
"references" option found.

@param GenerateSchemaTableEventArgs $args The arguments of the
ToolEvents::postGenerateSchemaTable
event
Parses the mapping and adds foreign - key constraints for each references option found .