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

Some Symfony 2 Command



# See the list of command:
  => php app/console list

# Create a new bundle for the application:
  => php app/console generate:bundle --namespace=Ens/JobeetBundle --format=yml

The generator will ask you some questions before generating the bundle. 
Here are the questions and the answers(all except one are the default answers):

  Bundle namespace [Ens/JobeetBundle]: Ens/JobeetBundle
  Bundle name [EnsJobeetBundle]: EnsJobeetBundle
  Target directory [/home/dragos/work/jobeet/src]: /home/dragos/work/jobeet/src
  Configuration format (yml, xml, php, or annotation) [yml]: yml
  Do you want to generate the whole directory structure [no]? yes
  Do you confirm generation [yes]? yes
  Confirm automatic update of your Kernel [yes]? yes
  Confirm automatic update of the Routing [yes]? yes

# Clear the cache after generating the new bundle with:
  => php app/console cache:clear --env=prod
  => php app/console cache:clear --env=dev

# Create the database using the information of app/config/parameters.ini file:
  => php app/console doctrine:database:create

# Generate the module class:
  => php app/console doctrine:generate:entities EnsJobeetBundle

# Create database table from the schema:
  => php app/console doctrine:schema:update --force
	
# Update the vendor libraries:
  => php bin/vendors install --reinstall
	
# Load the intial data for testing:
  => php app/console doctrine:fixtures:load
	
# Create a new controller (src/Ens/JobeetBundle/Controllers/JobController.php)
with actions for listing, creating, editing and deleting:
  => php app/console doctrine:generate:crud --entity=EnsJobeetBundle:Job 
                 --route-prefix=ens_job --with-write --format=yml
	
# Tell Symfony to make images, stylesheets and js available to the public:
  => php app/console assets:install web