0

有什么方法可以在 Doctrine 2 中制作备份表。

我有一个 XML,并且想每天通过 cron 作业导入它。首先,我创建一个以 _new 结尾的新模式。

if ($newTables) {
    $tablePastfix = new \DoctrineExtensions\TablePastfix('_new');
    $evm->addEventListener(\Doctrine\ORM\Events::loadClassMetadata, $tablePastfix);
    }

    $entityManager = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);

    if ($newTables) {
    $tool = new \Doctrine\ORM\Tools\SchemaTool($entityManager);
    $classes = array (
        $entityManager->getClassMetadata('MyClasses'),
        $entityManager->getClassMetadata('MyClasses'),
        $entityManager->getClassMetadata('MyClasses'),
    );
        $tool->createSchema($classes);
}

之后,我从 xml 导入数据。没关系,我重命名了表格

$dm = Doctrine\DBAL\DriverManager::getConnection($conn);
$sql = "DROP TABLE `mytable_old`;";
$stmt = $dm->query($sql);
$stmt->execute();

$dm = Doctrine\DBAL\DriverManager::getConnection($conn);
$sql = "ALTER TABLE `mytable` RENAME `mytable_old`;";
$stmt = $dm->query($sql);
$stmt->execute();

$dm = Doctrine\DBAL\DriverManager::getConnection($conn);
$sql = "ALTER TABLE `mytable_new` RENAME `mytable`;";
$stmt = $dm->query($sql);
$stmt->execute();

但是当我想再次导入时,出现以下错误。. . . . :(

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1005 Can't create table 'databasename.#sql-220_87' (errno: 121)' in /Applications/MAMP/bin/php/php5.3.14/lib/php/Doctrine/ORM/Tools/ToolsException.php on line 33

PDOException: SQLSTATE[HY000]: General error: 1005 Can't create table 'databasename.#sql-220_87' (errno: 121) in /Applications/MAMP/bin/php/php5.3.14/lib/php/Doctrine/DBAL/Connection.php on line 646

Call Stack:
    0.0011     761296   1. {main}() /path/to/jobs/import.php:0
    0.0014     767024   2. require_once('/path/to/jobs/config.php') /path/to/jobs/import.php:7
    0.0017     770536   3. require_once('/path/to/jobs/bootstrap.php') /path/to/jobs/config.php:3
    0.0020     796264   4. require_once('/path/to/jobs/bootstrap_doctrine.php') /path/to/jobs/bootstrap.php:5
    0.1468    7673040   5. Doctrine\ORM\Tools\SchemaTool->createSchema() /path/to/jobs/bootstrap_doctrine.php:53
    1.1216    9445080   6. Doctrine\DBAL\Connection->executeQuery() /Applications/MAMP/bin/php/php5.3.14/lib/php/Doctrine/ORM/Tools/SchemaTool.php:90
    1.1216    9445656   7. PDO->query() /Applications/MAMP/bin/php/php5.3.14/lib/php/Doctrine/DBAL/Connection.php:646

Doctrine\DBAL\DBALException: An exception occurred while executing 'ALTER TABLE doc_consultant_practice_new ADD CONSTRAINT FK_60C69DE344F779A2 FOREIGN KEY (consultant_id) REFERENCES doc_xml_consultant_new (id) ON DELETE CASCADE':

SQLSTATE[HY000]: General error: 1005 Can't create table 'databasename.#sql-220_87' (errno: 121) in /Applications/MAMP/bin/php/php5.3.14/lib/php/Doctrine/DBAL/DBALException.php on line 47

Call Stack:
    0.0011     761296   1. {main}() /path/to/jobs/import.php:0
    0.0014     767024   2. require_once('/path/to/jobs/config.php') /path/to/jobs/import.php:7
    0.0017     770536   3. require_once('/path/to/jobs/bootstrap.php') /path/to/jobs/config.php:3
    0.0020     796264   4. require_once('/path/to/jobs/bootstrap_doctrine.php') /path/to/jobs/bootstrap.php:5
    0.1468    7673040   5. Doctrine\ORM\Tools\SchemaTool->createSchema() /path/to/jobs/bootstrap_doctrine.php:53
    1.1216    9445080   6. Doctrine\DBAL\Connection->executeQuery() /Applications/MAMP/bin/php/php5.3.14/lib/php/Doctrine/ORM/Tools/SchemaTool.php:90

Doctrine\ORM\Tools\ToolsException: Schema-Tool failed with Error 'An exception occurred while executing 'ALTER TABLE doc_consultant_practice_new ADD CONSTRAINT FK_60C69DE344F779A2 FOREIGN KEY (consultant_id) REFERENCES doc_xml_consultant_new (id) ON DELETE CASCADE':

SQLSTATE[HY000]: General error: 1005 Can't create table 'databasename.#sql-220_87' (errno: 121)' while executing DDL: ALTER TABLE doc_consultant_practice_new ADD CONSTRAINT FK_60C69DE344F779A2 FOREIGN KEY (consultant_id) REFERENCES doc_xml_consultant_new (id) ON DELETE CASCADE in /Applications/MAMP/bin/php/php5.3.14/lib/php/Doctrine/ORM/Tools/ToolsException.php on line 33

Call Stack:
    0.0011     761296   1. {main}() /path/to/jobs/import.php:0
    0.0014     767024   2. require_once('/path/to/jobs/config.php') /path/to/jobs/import.php:7
    0.0017     770536   3. require_once('/path/to/jobs/bootstrap.php') /path/to/jobs/config.php:3
    0.0020     796264   4. require_once('/path/to/jobs/bootstrap_doctrine.php') /path/to/jobs/bootstrap.php:5
    0.1468    7673040   5. Doctrine\ORM\Tools\SchemaTool->createSchema() /path/to/jobs/bootstrap_doctrine.php:53

有人知道我的错误或有更好的想法来实现备份表吗?

4

1 回答 1

0

由于索引名称重复,您似乎遇到了问题,为表添加前缀不会为它们添加前缀。您应该为这种功能使用不同的数据库,您可以在之后重命名它或在打开两个连接的情况下同步数据。

于 2013-04-29T04:10:43.917 回答