使用doctrine从Symfony2中的外部数据库导入表

时间:2011-07-01 11:23:31

标签: doctrine symfony

我有一个带有自己数据库的Symfony2项目,现在我想连接到另一个数据库(另一个项目),所以我可以修改一些表。

我在config_dev.yml中创建了新连接

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver:   pdo_mysql
                host:     localhost
                dbname:   database1
                user:     root
                password: 
            buv:
                driver:   pdo_mysql
                host:     localhost
                dbname:   database2
                user:     root
                password:

我尝试使用以下命令导入架构:

  

$ php app / console doctrine:mapping:import --em = buv MyBundle yml

     

[学说\ DBAL \架构\ SchemaException]   表'old_table'

上不存在索引''

但是database2中的一些表没有PK!并且完全导入不起作用。但我只想导入两个表,所以我尝试了:

  

$ php app / console doctrine:mapping:import --em = buv --filter =“tablename”MyBundle yml

但是我得到了同样的错误,似乎--filter没有工作。

控制台命令doctrine中的文档:mapping:import仅表示将实体名称放在过滤器选项中。但我还没有实体。

9 个答案:

答案 0 :(得分:15)

如果我说得对,你想导入现有数据库吗?

我的工作是:

php app/console doctrine:mapping:convert xml ./src/App/MyBundle/Resources/config/doctrine/metadata/orm --from-database --force

然后选择性地转换为注释:

php app/console doctrine:mapping:import AppMyBundle annotation --filter="users_table"

如果您想要yml,请将注释更改为yml。

警告:当您导入到注释或yml时,它将删除您当前的实体文件。

答案 1 :(得分:15)

要求Doctrine 拥有标识符/主键。 请查看此页面:http://www.doctrine-project.org/docs/orm/2.0/en/reference/basic-mapping.html#identifiers-primary-keys

是一种从没有主键的表中生成映射和实体的方法。没有主键的表是一种不寻常和糟糕的数据库设计,但在遗留数据库的情况下存在这种情况。

<强>解决方案
注意:以下所有引用均参考Doctrine 2.0
1.找到文件 DatabaseDriver.php (在Doctrine / ORM / Mapping / Driver / DatabaseDriver.php中)
2.找到方法 reverseEngineerMappingFromDatabase 。修改如下所述的代码。
原始代码是:

private function reverseEngineerMappingFromDatabase()
    {
        if ($this->tables !== null) {
            return;
        }

        $tables = array();

        foreach ($this->_sm->listTableNames() as $tableName) {
            $tables[$tableName] = $this->_sm->listTableDetails($tableName);
        }

        $this->tables = $this->manyToManyTables = $this->classToTableNames = array();
        foreach ($tables as $tableName => $table) {
            /* @var $table \Doctrine\DBAL\Schema\Table */
            if ($this->_sm->getDatabasePlatform()->supportsForeignKeyConstraints()) {
                $foreignKeys = $table->getForeignKeys();
            } else {
                $foreignKeys = array();
            }

            $allForeignKeyColumns = array();
            foreach ($foreignKeys as $foreignKey) {
                $allForeignKeyColumns = array_merge($allForeignKeyColumns, $foreignKey->getLocalColumns());
            }

            if ( ! $table->hasPrimaryKey()) {
                throw new MappingException(
                    "Table " . $table->getName() . " has no primary key. Doctrine does not ".
                    "support reverse engineering from tables that don't have a primary key."
                );
            }

            $pkColumns = $table->getPrimaryKey()->getColumns();
            sort($pkColumns);
            sort($allForeignKeyColumns);

            if ($pkColumns == $allForeignKeyColumns && count($foreignKeys) == 2) {
                $this->manyToManyTables[$tableName] = $table;
            } else {
                // lower-casing is necessary because of Oracle Uppercase Tablenames,
                // assumption is lower-case + underscore separated.
                $className = $this->getClassNameForTable($tableName);
                $this->tables[$tableName] = $table;
                $this->classToTableNames[$className] = $tableName;
            }
        }
    }


修改后的代码是:

private function reverseEngineerMappingFromDatabase()
    {
        if ($this->tables !== null) {
            return;
        }

        $tables = array();

        foreach ($this->_sm->listTableNames() as $tableName) {
            $tables[$tableName] = $this->_sm->listTableDetails($tableName);
        }

        $this->tables = $this->manyToManyTables = $this->classToTableNames = array();
        foreach ($tables as $tableName => $table) {
            /* @var $table \Doctrine\DBAL\Schema\Table */
            if ($this->_sm->getDatabasePlatform()->supportsForeignKeyConstraints()) {
                $foreignKeys = $table->getForeignKeys();
            } else {
                $foreignKeys = array();
            }

            $allForeignKeyColumns = array();
            foreach ($foreignKeys as $foreignKey) {
                $allForeignKeyColumns = array_merge($allForeignKeyColumns, $foreignKey->getLocalColumns());
            }

            $pkColumns=array();
            if ($table->hasPrimaryKey()) {
                $pkColumns = $table->getPrimaryKey()->getColumns();
                sort($pkColumns);
            }

            sort($allForeignKeyColumns);

            if ($pkColumns == $allForeignKeyColumns && count($foreignKeys) == 2) {
                $this->manyToManyTables[$tableName] = $table;
            } else {
                // lower-casing is necessary because of Oracle Uppercase Tablenames,
                // assumption is lower-case + underscore separated.
                $className = $this->getClassNameForTable($tableName);
                $this->tables[$tableName] = $table;
                $this->classToTableNames[$className] = $tableName;
            }
        }
    }


3.在同一文件中找到 loadMetadataForClass 方法。修改如下所述的代码。
找到下面陈述的代码:

try {
   $primaryKeyColumns = $this->tables[$tableName]->getPrimaryKey()->getColumns();
} catch(SchemaException $e) {
    $primaryKeyColumns = array();
}


修改如下:

try {
     $primaryKeyColumns = ($this->tables[$tableName]->hasPrimaryKey())?$this->tables[$tableName]->getPrimaryKey()->getColumns():array();
} catch(SchemaException $e) {
     $primaryKeyColumns = array();
}



即使对于没有主键的表,上述解决方案也会创建映射(xml / yml / annotation)。

答案 2 :(得分:4)

我已经通过在doctrine dbal config(schema_filter)中添加~/app/config/config.yml成功导入了一些数据库实体

# Doctrine Configuration
doctrine:
    dbal:
        driver:   %database_driver%
        host:     %database_host%
        port:     %database_port%
        dbname:   %database_name%
        user:     %database_user%
        password: %database_password%
        charset:  UTF8
        schema_filter: /^users_table/

app/console doctrine:mapping:import --force MyBundle yml

然后还原config.yml。

答案 3 :(得分:3)

我基于简化代码的所有注释创建了一个解决方案

上课时间 namespace Doctrine \ ORM \ Mapping \ Driver; DatabaseDriver.php

在第277行,更改:

if (!$table->hasPrimaryKey()) {
      // comment this Throw exception
      // throw new MappingException(
      // “Table “ . $table->getName() . “ has no primary key.
      // Doctrine does not “.
      // “support reverse engineering from tables that don’t
      // have a primary key.”
      // );
} else {
     $pkColumns = $table->getPrimaryKey()->getColumns();
}

并且,在第488行,添加:

if( $table->hasPrimaryKey() ) //add this if to avoid fatalError
 return $table->getPrimaryKey()->getColumns();

为避免将来出现任何问题,请在映射数据库后返回设置以避免以后出现任何问题。 祝你好运!

答案 4 :(得分:3)

请注意,您的命令中的--filter应填充实体类名称,而不是名称。如果实体尚不存在,则实体类名称必须与您的表名称相符。因此,如果您的表格为user_table,则过滤器值为UserTable

然后要解决您的数据库有一些Doctrine无法处理的表,您应该将您希望允许Doctrine管理的表列入白名单。您可以在配置文件中执行此操作,例如:

doctrine:
    dbal:
        # ... 
        schema_filter: /^(users_table|emails)$/

或者您可以在cli-config.php文件中指定它。

/** @var Doctrine\ORM\Configuration $config */
$config->setFilterSchemaAssetsExpression('/^(users_table|email)$/');

答案 5 :(得分:1)

您必须将getTablePrimaryKeys函数更新为:

private function getTablePrimaryKeys(Table $table)
{
    try {       
        $primaryKeyColumns = ($this->tables[$table->getName()]->hasPrimaryKey())?$this->tables[$table->getName()]->getPrimaryKey()->getColumns():array();
    } catch(SchemaException $e) {
        $primaryKeyColumns = array();
    }

    return array();
}

答案 6 :(得分:0)

在DatabaseDriver.php文件reverseEngineerMappingFromDatabase函数中,您可以更改

throw new MappingException("Table " . $table->getName() . " has no primary key. Doctrine does not "."support reverse engineering from tables that don't have a primary key.");

if(! $table->hasColumn('id')){
   $table->addColumn('id', 'integer', array('autoincrement' => true));
}
   $table->setPrimaryKey(array('id'));

答案 7 :(得分:0)

尝试至少临时更改您的学说配置

doctrine:
    dbal:
        schema_filter: ~^(?!table1|table2)~

然后运行:

php bin/console doctrine:mapping:convert --force --from-database --filter="CamelCasedTableName" --namespace="App\Entity\\" annotation   .

花了很多时间找到这个解决方案。 考特尼·迈尔斯 (Courtney Miles) 奇怪但上层的回答对我不起作用。这个至少在 symfony 4.4 上运行良好

答案 8 :(得分:-1)

php bin/console doctrine:mapping:convert xml ./src/NameBundle/Resources/doctrine/metadata/orm

php bin/console doctrine:mapping:import NameBundle yml

php bin/console doctrine:generate:entities NameBundle