Symfony 4 Doctrine 2,Mapping实体,错误表没有主键,

时间:2018-04-09 16:29:36

标签: php symfony doctrine

这里有一个symfony noob。从早上开始尝试使用Doctrine通过控制台映射数据库的实体,引发no primary key错误。 mysql服务器是一个远程服务器,我很遗憾只有读访问权限,我无法创建主键。我确实在SO上看到了一些关于完全相同问题的问题,但是他们没有得到答复和陈旧。

我试过https://medium.com/@joaoneto/solves-doctrine-orm-error-with-tables-without-primary-key-on-mysql-when-mapping-the-database-1ce740610b51 但是它又引发了关于空列的错误。

  Call to a member function getColumns() on null 

我的学说.yaml。显然我改变了连接细节。

doctrine:
  dbal:
    default_connection: default
    connections:
      # configure these for your database server
      default:
        driver: 'pdo_mysql'
        host: 'localhost'
        port: '3306'
        dbname: 'symfony_test_db'
        user: 'root'
        password: ''
        charset: utf8mb4

      customer:
        driver: 'pdo_mysql'
        host: 'xxx.xxx.xx.xxx'
        port: '3306'
        dbname: 'sg3_symfony_db'
        user: 'sguser'
        password: 'password'
        charset: UTF8

      backoffice:
        driver: 'pdo_mysql'
        host: 'localhost'
        port: '3306'
        dbname: 'back_office'
        user: 'backoffice_user'
        password: 'password'
        charset: UTF8


      one:
        driver: 'pdo_mysql'
        host: 'xxx.xxx.xx.xxx'
        port: '3306'
        dbname: 'one_db'
        user: 'one_user'
        password: 'password'
        charset: UTF8


      staging:
        driver: 'pdo_mysql'
        host: 'xxx.xxx.xx.xxx'
        port: '3306'
        dbname: 'staging'
        user: 'staginguser'
        password: 'password'
        charset: UTF8



      # With Symfony 3.3, remove the `resolve:` prefix
      #url: '%env(resolve:DATABASE_URL)%'
orm:

    default_entity_manager: default
    entity_managers:

      default:
        connection: default
        #auto_mapping: true
        mappings:
          Main:
              is_bundle: false
              type: annotation
              dir: '%kernel.project_dir%/src/Entity/Main'
              prefix: 'App\Entity\Main'
              alias: Main

      customer:
        connection: customer

        mappings:
          Customer:
              is_bundle: false
              type: annotation
              dir: '%kernel.project_dir%/src/Entity/Customer'
              prefix: 'App\Entity\Customer'
              alias: Customer


      backoffice:
        connection: backoffice

        mappings:
          Backoffice:
              is_bundle: false
              type: annotation
              dir: '%kernel.project_dir%/src/Entity/Backoffice'
              prefix: 'App\Entity\Backoffice'
              alias: Backoffice


      one:
        connection: mt4

        mappings:
          One:
              is_bundle: false
              type: annotation
              dir: '%kernel.project_dir%/src/Entity/One'
              prefix: 'App\Entity\One'
              alias: One



      staging:
        connection: staging

        mappings:
          staging:
              is_bundle: false
              type: annotation
              dir: '%kernel.project_dir%/src/Entity/Staging'
              prefix: 'App\Entity\Staging'
              alias: Staging

我使用CLI命令进行映射但失败。

php bin/console doctrine:mapping:convert  --from-database annotation --force  --em=one ./src/Entity/One/ --verbose

2 个答案:

答案 0 :(得分:0)

不是一个非常干净的解决方案,但目前我设法做到了,导出我需要的表的架构并在我的本地服务器上重新创建它们。在未定义PK的表上强制执行Pk。这创建了实体类文件,并且像魅力一样工作。

答案 1 :(得分:0)

在这种情况下,有时您需要四处走走。

这个错误:

<块引用>

表____没有主键。 Doctrine 不支持反向 从没有主键的表中进行工程设计。

所以。
Doctrine 不能在没有主键的表上工作。
在 MySQL 中,创建没有 PK 的表总是一个坏主意,但是,在某些情况下(或遗留系统),某些表上没有 PK,您仍然可以使用 Doctrine 作为 ORM。

但是,默认情况下(我相信这不会改变)如果您的数据库有没有主键映射的表将根本无法工作。

解决这个问题的更快捷的方法是覆盖命名空间中的供应商类 DatabaseDriver:

namespace Doctrine\ORM\Mapping\Driver; 
    On line 289, change:
                if ( ! $table->hasPrimaryKey()) {
                continue;
//                throw new MappingException(
//                    "Table " . $table->getName() . " has no primary key. Doctrine does not ".
//                    "support reverse engineering from tables that don't have a primary key."
//                );
            }

为避免将来出现任何问题,请在映射数据库后返回设置以避免以后出现任何问题。

祝你好运!

参考资料Solves Doctrine ORM Error with tables without Primary Key on MySQL when mapping the database.