Symfony 3, Add A New Column To Database Table Using Doctrine Command


Modify you metadata file with new column name that you are going to add/modify. For example i have a user table metadata file and want to add a phone column. The metadata file is in yml format. Metadata files are found under the following path


src/AppBundle/Resources/config/doctrine

lets open the file that you want to modify and add the following lines:


phone:
    type: string
    nullable: true
    length: 255
    options:
        fixed: false    

Now Run the following doctrine command to update your Entity class based on the changes at your metadata file. Here i am updating only my Users Entity Class.



php bin/console doctrine:generate:entities AppBundle:Users


Now run the following doctrine command to update your database table with the modification you have done yet.



php bin/console doctrine:schema:update --force


Advertisements

Symfony 3 Generate Entity Class From an Existing database using Doctrine Command


Generate Metadata files using Doctrine command, Metadata files describe the entity class to generate based on database tables. You can generate xml or yml format.


php bin/console doctrine:mapping:import --force AppBundle xml

OR

php bin/console doctrine:mapping:import --force AppBundle yml

You can see your metadata files are created under the following path based on the database tables you have in your database.


src/AppBundle/Resources/config/doctrine

Once Metadata files are generated, now you can create your entity classes using the following Doctrine command. Following two command will generate the Entity classes for each table including annotation, setter and getter methods.


php bin/console doctrine:mapping:convert annotation ./src

php bin/console doctrine:generate:entities AppBundle

Now you can see the created Entity files under following directory.


src/AppBundle/Entity