【问题标题】:Symfony 4 Doctrine 2, Mapping entity, error table has no primary key,Symfony 4 Doctrine 2,映射实体,错误表没有主键,
【发布时间】:2018-09-19 02:58:02
【问题描述】:

这里是一个 symfony 菜鸟。从早上开始就一直在尝试通过控制台使用 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

【问题讨论】:

  • 你能告诉我们你的实体和代码吗?
  • @AlessandroMinoccheri 我已经用连接设置更新了我的帖子。我没有实体代码,因为这是我试图生成的导致此错误的代码
  • 你的帖子没有问题。
  • @Emil 不确定您是否理解我的帖子,但如果您仔细阅读,我想问当我的某些表没有主键时如何从 cli 生成实体类文件。如何绕过关于某些表上没有 pk 的教义错误。

标签: php symfony doctrine


【解决方案1】:

不是一个非常干净的解决方案,但目前我设法通过导出我需要的表的架构并在我的本地服务器上重新创建它们来做到这一点。在没有定义 PK 的表上强制使用 Pk。这会立即创建实体类文件,并且效果很好。

【讨论】:

    【解决方案2】:

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

    这个错误:

    表____没有主键。教义不支持反向 从没有主键的表中进行工程。

    所以。
    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.

    【讨论】:

      猜你喜欢
      • 2016-05-10
      • 2013-03-26
      • 1970-01-01
      • 2019-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多