2

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 
4
  • can you show us your entity and code? Commented Apr 10, 2018 at 13:43
  • @AlessandroMinoccheri I have updated my post with connection settings. I do not have entity code as that is what I am trying to generate which results in this error Commented Apr 10, 2018 at 14:05
  • Your post contains no question. Commented Apr 10, 2018 at 23:19
  • @Emil Not sure if you did understand my post but if you read it carefully , I am asking how to generate entity class files from the cli when some of my tables does not have a primary key. how to bypass the doctrine error thrown regarding no pk's on some tables. Commented Apr 11, 2018 at 9:18

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This solution ignores the tables without primary keys. The solution in the article includes them.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.