Symfony 2 Doctrine MS SQL 错误

Symfony 2 Doctrine MS SQL Error

我已经开始进行 Symfony 2.3 项目,我需要将用经典 Asp 编写的金融网站转换为 Php。

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

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

Doctrine\DBAL\DBALException Unknown database type timestamp requested, Doctrine\DBAL\Platforms\SQLServer2008Platform may not support it

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

请帮助我,因为我是 Symfony 2.3 的新手,谢谢

您可以按照有关主题的学说文档添加自己的类型:

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

这是关于枚举类型,但您可以将它与时间戳等任何类型一起使用,link它可以用于字符串类型或您的数据库支持的任何其他最适合您的类型。

src/My/Project/MyProjectBundle.php

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

通常所有时间戳类型都应翻译为字符串。

下面的配置适用于 Synfony 3.2

1) 在config.yml中在types下添加新类型并映射到mssql connection

# 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) 创建 class AppBundle\Type\Timestamp 如下 url:

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