我有一个带有自己的数据库的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:
Run Code Online (Sandbox Code Playgroud)
我尝试使用以下命令导入架构:
$ php app/console doctrine:mapping:import --em = buv MyBundle yml
[Doctrine\DBAL\Schema\SchemaException]表'old_table'上不存在索引''
但是database2中的一些表没有PK!并且完全导入不起作用.但我只想导入两个表,所以我试过:
$ php app/console doctrine:mapping:import --em = buv --filter ="tablename"MyBundle yml
但我得到了同样的错误,似乎--filter不工作.
控制台命令doctrine:mapping:import中的文档仅表示将实体名称放在过滤器选项中.但我还没有实体.
jon*_*nv1 15
如果我找到你,你想导入你现有的数据库?
我所做的是:
php app/console doctrine:mapping:convert xml ./src/App/MyBundle/Resources/config/doctrine/metadata/orm --from-database --force
Run Code Online (Sandbox Code Playgroud)
然后选择性转换为注释:
php app/console doctrine:mapping:import AppMyBundle annotation --filter="users_table"
Run Code Online (Sandbox Code Playgroud)
如果您想要yml,请将注释更改为yml.
警告:当您导入到注释或yml时,它将删除您当前的实体文件.
Rob*_*zvi 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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
修改后的代码是:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
3. 在同一文件中找到方法loadMetadataForClass.修改如下所述的代码.
找到下面陈述的代码:
try {
$primaryKeyColumns = $this->tables[$tableName]->getPrimaryKey()->getColumns();
} catch(SchemaException $e) {
$primaryKeyColumns = array();
}
Run Code Online (Sandbox Code Playgroud)
像这样修改它:
try {
$primaryKeyColumns = ($this->tables[$tableName]->hasPrimaryKey())?$this->tables[$tableName]->getPrimaryKey()->getColumns():array();
} catch(SchemaException $e) {
$primaryKeyColumns = array();
}
Run Code Online (Sandbox Code Playgroud)
即使对于没有主键的表,上述解决方案也会创建映射(xml/yml/annotation).
我已经通过schema_filter在 dotric dbal config ( ~/app/config/config.yml) 中添加 a 成功导入了一些数据库实体
# 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/
Run Code Online (Sandbox Code Playgroud)
app/console doctrine:mapping:import --force MyBundle yml
然后恢复 config.yml。