Symfony2 Doctrine2 - generate One-To-Many annotation from existing database by doctrine:mapping:import

前端 未结 1 999
说谎
说谎 2020-12-16 08:19

I want to generate Entities from an Existing Database by using Doctrine tools for reverse engineering

/*
 * SET FOREIGN_KEY_CHECKS=0;
  -- -----------------         


        
1条回答
  •  执念已碎
    2020-12-16 08:40

    Simply this is change inside doctrine orm lib,

    you can fix that by changes in

    vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php

    replace the following statements

    foreach ($foreignKeys as $foreignKey) {
              $foreignTable = $foreignKey->getForeignTableName();
              $cols = $foreignKey->getColumns();
              $fkCols = $foreignKey->getForeignColumns();
    
              $localColumn = current($cols);
              $associationMapping = array();
              $associationMapping['fieldName'] = $this->getFieldNameForColumn($tableName, $localColumn, true);
              $associationMapping['targetEntity'] = $this->getClassNameForTable($foreignTable);
    
              if ($primaryKeyColumns && in_array($localColumn, $primaryKeyColumns)) {
              $associationMapping['id'] = true;
              }
    
              for ($i = 0; $i < count($cols); $i++) {
              $associationMapping['joinColumns'][] = array(
              'name' => $cols[$i],
              'referencedColumnName' => $fkCols[$i],
              );
              }
    
              //Here we need to check if $cols are the same as $primaryKeyColums
              if (!array_diff($cols, $primaryKeyColumns)) {
              $metadata->mapOneToOne($associationMapping);
              } else {
              $metadata->mapManyToOne($associationMapping);
              }
              }
    

    change to

    foreach ($foreignKeys as $foreignKey) {
                $foreignTable = $foreignKey->getForeignTableName();
                $cols = $foreignKey->getColumns();
                $fkCols = $foreignKey->getForeignColumns();
    
                $localColumn = current($cols);
                $associationMapping = array();
                $associationMapping['fieldName'] = $this->getFieldNameForColumn($tableName, $localColumn, true);
                $associationMapping['targetEntity'] = $this->getClassNameForTable($foreignTable);
    
                for ($i = 0; $i < count($cols); $i++) {
                    $associationMapping['joinColumns'][] = array(
                        'name' => $cols[$i],
                        'referencedColumnName' => $fkCols[$i],
                    );
                }
                $metadata->mapManyToOne($associationMapping);
            }
    
            foreach ($this->tables as $tableCandidate) {
                if ($this->_sm->getDatabasePlatform()->supportsForeignKeyConstraints()) {
                    $foreignKeysCandidate = $tableCandidate->getForeignKeys();
                } else {
                    $foreignKeysCandidate = array();
                }
    
                foreach ($foreignKeysCandidate as $foreignKey) {
                    $foreignTable = $foreignKey->getForeignTableName();
    
                    if ($foreignTable == $tableName && !isset($this->manyToManyTables[$tableCandidate->getName()])) {
    
                        $fkCols = $foreignKey->getForeignColumns();
                        $cols = $foreignKey->getColumns();
    
    
                        $localColumn = current($cols);
    
                        $associationMapping = array();
                        $associationMapping['fieldName'] = $this->getFieldNameForColumn($tableCandidate->getName(), $tableCandidate->getName(), true);
                        $associationMapping['targetEntity'] = $this->getClassNameForTable($tableCandidate->getName());
                        $associationMapping['mappedBy'] = $this->getFieldNameForColumn($tableCandidate->getName(), $localColumn, true);
    
                        try {
                            $primaryKeyColumns = $tableCandidate->getPrimaryKey()->getColumns();
                            if (count($primaryKeyColumns) == 1) {
                                $indexColumn = current($primaryKeyColumns);
                                $associationMapping['indexBy'] = $indexColumn;
                            }
                        } catch (SchemaException $e) {
    
                        }
    
                        $metadata->mapOneToMany($associationMapping);
                    }
                }
            }
    

    now if you run doctrine:mapping:import

    you will find OneToMany annotation.

    then run doctrine:generate:entities

    you will find A unidirectional relationship on both sides.

    0 讨论(0)
提交回复
热议问题