yozawiratama
yozawiratama

Reputation: 4318

Symfony 4 getter setter not generated from existing database

I know that to generate entity from existing database in symfony 4, just execute this command :

> php bin/console doctrine:generate:entities

but this command dont generate getter setter, just generate variable from column.

and of course I must use some dirty work to create it manually.

may be I missed read the documentation, how to generate entities getter setter from existing database using doctrine symfony 4?

Upvotes: 4

Views: 10718

Answers (3)

M. Galardi
M. Galardi

Reputation: 130

To do that from an existing database you can use reverse engineering. Follow these steps:

  1. configure your database in .env file

2.1. create the entity from your configured database with:

php bin/console doctrine:mapping:convert --from-database annotation ./src/Entity

2.2. if you want create the entity for one specific table:

php bin/console doctrine:mapping:convert --from-database --filter="Tablename" annotation ./src/Entity
  1. if you use vim, you can add getters and setters installing php-getter-setter.vim plugin and typing in each entity:

    :% InsertBothGetterSetter

  2. you must add manually in each entity:

    namespace App\Entity;

Upvotes: 0

To generate entity classes from an existing database you need to ask Doctrine to introspect the database and generate the corresponding metadata files. Metadata files describe the entity class to generate based on table fields.

> php bin/console doctrine:mapping:import App\\Entity annotation --path=src/Entity

This command will generate new PHP classes with annotation metadata into src/Entity To generate the missing getter/setter methods (or to create the classes if neceesary), run:

> php bin/console make:entity --regenerate App

Also check Official documentation

Upvotes: 13

Roubi
Roubi

Reputation: 2106

DOC says: "If you prefer to add new properties manually, the make:entity command can generate the getter & setter methods for you:

php bin/console make:entity --regenerate

If you make some changes and want to regenerate all getter/setter methods, also pass --overwrite."

Also note that with Symfony 3 (I don't about v4), the doctrine:generate:entities did not work with protected properties

Upvotes: 1

Related Questions