douhan6738
2015-08-09 12:21
浏览 40
已采纳

Symfony2 - doctrine获取表创建顺序

I am using Symfony 2.7, doctrine 2, MySQL. I'm trying to retrieve the tables creation order in a controller as when issuing

php app/console doctrine:generate:schema --dump-sql

but I only need the table names. So, for instance, if I have two tables like Product - Category I'd like to have an output which looks like this : array('Category', 'Product')

Using this documentation here's what I've done so far:

public function getTablesCreationOrderAction()
{
    $conn = $this->get('database_connection');
    $sm = $conn->getSchemaManager();
    $sequences = $sm->listSequences($this->getParameter('database_name'));
    die();
}

An exception is thrown at $sequences = $sm->listSequences.

Here's the exception I get:

Operation 'Doctrine\DBAL\Platforms\AbstractPlatform::getListSequencesSQL' is not supported by platform.

I don't know if this means that MySQL does not support the operation.

Thanks!

图片转代码服务由CSDN问答提供 功能建议

我正在使用Symfony 2.7,doctrine 2,MySQL。 我正在尝试检索表中的表创建顺序 发布时的控制器

php app / console doctrine:generate:schema --dump-sql

但我只需要表名。 例如,如果我有两个表,如 产品 - 类别我想要一个如下所示的输出:array('Category ','产品')

使用这个文档这是我到目前为止所做的:

  public function getTablesCreationOrderAction()
 {
 $ conn = $ this->  get('database_connection'); 
 $ sm = $ conn-> getSchemaManager(); 
 $ sequences = $ sm-> listSequences($ this-> getParameter('database_name')); 
 die(  ); 
} 
   
 
 

$ sequences = $ sm-> listSequences抛出异常。

这里是 我得到的例外:

平台不支持操作'Doctrine \ DBAL \ Platforms \ AbstractPlatform :: getListSequencesSQL'。

我不知道这是否意味着MySQL不支持该操作。

谢谢!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanchangnie7996 2015-08-17 17:36
    已采纳

    For those interested, here's how I did it. There may be better ways..

    public function getTablesCreationOrderAction()
    {
        $app = New Application($this->get('kernel'));
        $app->setAutoExit(false);
    
        $input = New ArrayInput(['command' => 'doctrine:schema:create',
                                 '--dump-sql'=>true]);
        $output = New BufferedOutput();
    
        $app->run($input, $output);
        $rst = $output->fetch();
    
        $schema = explode('CREATE TABLE ', $rst);
    
        $tables = [];
        foreach ($schema as $tableDef)
        {
            $tables[] = explode(' (', $tableDef)[0];
        }
    
        unset($tables[0]); // empty
        var_dump($tables);
        die();
    }
    
    点赞 评论
  • dsfdsf46465 2015-08-09 13:15

    Yes, but it seems like the feature isn't implemented yet, since this is in the source code of AbstractPlatform you will see this:

    public function getListSequencesSQL($database)
    {
        throw DBALException::notSupported(__METHOD__);
    }
    

    Now this means that platforms overriding AbstractPlatform must override these functions to provide the right implementation.
    Now if you take a look at MySqlPlatform, you won't be able to find the getListSequencesSQL() anywhere, hence you can't use this feature with mysql or at least it's not implemented.

    点赞 评论

相关推荐 更多相似问题