Symfony 2 Doctrine MS SQL 错误

4

我开始着手一个Symfony 2.3项目,需要将一个经典的Asp编写的金融网站转换为Php。

客户有一个Ms-Sql数据库,需要与Symfony 2.3和doctrine一起使用,我已经创建了数据库并从模式文件中加载了空表。

现在每当我尝试从数据库(Ms-Sql)生成实体时,就会出现以下错误:

Doctrine\DBAL\DBALException 未知的数据库类型时间戳请求,Doctrine\DBAL\Platforms\SQLServer2008Platform可能不支持它

是否有人知道如何解决这个问题或者该怎么做才能避免这种情况?

由于我刚接触Symfony 2.3,请帮助我,谢谢

2个回答

4
您可以按照Doctrine文档的主题添加自己的类型:

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/mysql-enums.html

这涉及到枚举类型,但是您可以将其与任何类型一起使用,如时间戳,并将其链接到字符串类型或任何其他由数据库支持的最适合您的类型。

src/My/Project/MyProjectBundle.php

public function boot()
{
    $em = $this->container->get('doctrine.orm.entity_manager');
    $em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('timestamp','string');
}

通常情况下,所有的时间戳类型都应该被翻译为字符串。


1
对于 doctrine:schema:validate 在 datetime2 失败的情况下,以下解决方案可能有用:未知数据库类型 datetime2 请求,Realestate\MssqlBundle\Platforms\DblibPlatform - Matteo
我必须通过在控制台中编写以下命令来从数据库表生成实体:php app/console doctrine:mapping:import --force AcmeBlogBundle yml。 - Robin
此时实体未生成,Doctrine 显示了我在问题中发布的错误。如何解决? - Robin
1
@Robin,在映射之前,你将我发布的函数添加到了你的AcmeBlogBundle.php中吗? - Nawfal Serrar
哦,抱歉我在控制器中使用了它。我的道歉。 - Robin
显示剩余2条评论

0

以下配置适用于Symfony 3.2

1)在config.yml中,在types下添加新类型,并将其映射到mssql连接下

# Doctrine Configuration
doctrine:
    dbal:
        types:
            timestamp:  AppBundle\Type\Timestamp
..
..

        connections:
            mssql:
                driver:   sqlsrv
                host:     "%mssql_host%"
                port:     "%mssql_port%"
                dbname:   "%mssql_db_name%"
                user:     "%mssql_user%"
                password: "%mssql_password%"
                mapping_types:
                    timestamp: timestamp

2) 根据以下URL创建类AppBundle\Type\Timestamp:

https://github.com/mmerian/doctrine-timestamp/blob/master/lib/DoctrineTimestamp/DBAL/Types/Timestamp.php

3)通过以下命令从数据库生成实体:

php bin/console doctrine:mapping:import --force AppBundle xml --em=mssql


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接