简体   繁体   中英

Symfony 4 Doctrine 2, Mapping entity, error table has no primary key,

A symfony noob here. Been trying since morning to map entity/s of the database using Doctrine via console throws up no primary key error. The mysql server is a remote one which I unfortunately only have read access and I am not able to create a primary key. I did see a few questions on SO with the exact same issue but they were unanswered and old.

I tried https://medium.com/@joaoneto/solves-doctrine-orm-error-with-tables-without-primary-key-on-mysql-when-mapping-the-database-1ce740610b51 but again it throws up error regarding empty columns.

  Call to a member function getColumns() on null 

My doctrine.yaml. Obviously I altered the connection details.

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 command I use to map but fails.

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

Not a very clean solution but for the moment I managed to do it by, exporting the schema of tables I need and recreating them on my local server. Forcing Pk's on the tables that did not have PK defined. This created entity class files instantly and worked like a charm.

In this case, sometimes you need to walk around.

This error:

Table ____ has no primary key. Doctrine does not support reverse engineering from tables that don't have a primary key.

So.
Doctrine can not work on tables that have no primary key.
In MySQL, create tables without PK will always be a bad idea, but, in some cases (or legacy systems) that not have PKs on some tables, you still can use Doctrine as ORM.

However, by default (and I believe this will not change) if your database has tables without primary key Mapping simply will not work.

The more fast way to solve this is override the vendor class DatabaseDriver, in the namespace:

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."
//                );
            }

To avoid any future problems, after mapping your database, return the settings to avoid any problems later.

Good luck!

Reference : Solves Doctrine ORM Error with tables without Primary Key on MySQL when mapping the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM