TYPO3 使用 Querybuilder 更改表

问题描述 投票:0回答:4

如何使用 TYPO3 9 Querybuilder 执行“ALTER TABLE”命令?

ALTER TABLE foo
  DROP INDEX bar;

这个类应该用来执行查询:

use TYPO3\CMS\Core\Database\ConnectionPool;

...
$table = 'foo';
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);

或者是否有必要实例化另一个数据库对象,例如派生自 Doctrine\DBAL\Schema\Schema 类。

typo3 query-builder
4个回答
3
投票

这应该可以完成工作:

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;

// ...

GeneralUtility::makeInstance(ConnectionPool::class)
    ->getConnectionForTable('foo')
    ->exec('ALTER TABLE foo DROP INDEX bar;');

文档警告有关在存储库之外使用查询的信息: https://docs.typo3.org/m/typo3/book-extbasefluid/master/en-us/6-Persistence/3-implement-individual-database-queries.html


0
投票

尝试一下,这可能对你有帮助。但是,我以前从未尝试过更改表命令(从不需要!!)

$connection = $this->objectManager->get(ConnectionPool::class)->getConnectionForTable($table);
$statement = $this->objectManager->get(
                   \Doctrine\DBAL\Statement::class
                   'ALTER TABLE foo DROP INDEX bar',
                   $connection
             );

$query = $this->createQuery();
$query->statement($statement);

0
投票

另一种解决方案是在这种情况下使用 Doctrine Schema Manager:

use \TYPO3\CMS\Core\Utility\GeneralUtility;
use \TYPO3\CMS\Core\Database\ConnectionPool;

$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('foo');
$connection->getSchemaManager()->dropIndex('bar', 'foo');

0
投票

对于 TYPO3v12 及更高版本,您可以使用:

$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('foo');
$connection->executeStatement('ALTER TABLE DROP INDEX bar');

(注意:如果这样做,您将编写特定于数据库的查询,并且您将被锁定到该 SQL 有效的数据库变体。)

© www.soinside.com 2019 - 2024. All rights reserved.