Symfony2 Console Cheat Sheet

Symfony2 has many useful command lines to help you. The command lines can assist you with Symfony2 development and saves time when you are coding. I have listed below a few of the commands that I know of with details of how they can be used. If you know of a command that is missing from the list, please feel free to add it in the comments.

To run these commands you must open CMD (window) or Terminal (MAC) and navigate to your Symfony2 root directory.

  • Create a new bundle

    php app/console generate:bundle --namespace=[namespace]/[bundle name]Bundle --format=[bundle configuration format]


    php app/console generate:bundle --namespace=Moo/UserBundle --format=yml

    namespace is your company name the prefix that will identify your bundles from other bundles that you may have in your application.

    bundle name is what is it for? Examples of bundles: Blog bundle, User management bundle, Forum bundle, and so on. Bundle does not have to be accessbile from the web, you could create a bundle to manage internal functions such as integrating jQuery framework. You can create as many as you like of bundles under your company namespace

    There are 3 types of configuration format that you can choose from YML, XML & PHP.

  • Web resources. CSS, javascript & images

    php app/console assets:install web --symlink


    php app/console assets:install web

    As I said before bundles can be to manage internal functions (The jQuery bundle). These resources must be located under the directory

    /src/[namespace]/[bundle name]Bundle/Resources/public/

    This command is going to copy your files or create symlinks under

    /web/bundles/[namespace][bundle name]

  • Clear enviroment cache

    php app/console cache:clear --env=[enviroment name]


    php app/console cache:clear --env=prod

    –env is the name of the enviroment you want to remove its cache. Each subdirectory under

    /app/cache/

    is an enviroment.

  • Install/Upgrade bundles and Symfony2

    php bin/vendors install

    This commands installs or upgrade the bundles defined in the file

    /deps

  • Create database with doctrine2

    php app/console doctrine:database:create

    This command will create the application database based on the parameters defined in

    app/config/parameters.ini

    This command will not create tables, it just create an empty database if it does not exists else it will throw an error.

  • Mapping doctrine2 entities with the database

    php app/console doctrine:generate:entities [namespace]


    php app/console doctrine:generate:entities Moo

    You can run this command after changes to the ORM metadata for the entity classes. It will create getter and setter methods. But it will not modify any of the existing methods.

  • Create tables in the database with doctrine2

    php app/console doctrine:schema:create

    Based on the defined ORM metadata this command will create all of the tables.

    If you don’t want to create the tables but return a dump of the SQL queries, then pass the following option

    --dump-sql

    to the right side of the command.

  • Load fixtures with doctrine2

    php app/console doctrine:fixtures:load

    Pre-requisition to install doctrine fixtures and DoctrineFixturesBundle.

    [doctrine-fixtures]
        git=http://github.com/doctrine/data-fixtures.git
    
    [DoctrineFixturesBundle]
        git=http://github.com/symfony/DoctrineFixturesBundle.git
        target=/bundles/Symfony/Bundle/DoctrineFixturesBundle

    Then upgrade vendors to install the new bundles.

  • Update schema with doctrine2

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

    This command will update the databse schema, if you have made some changes to a bundle entities and would like to update the schema accordenly.

  • Data migrations with doctrine2

    php app/console doctrine:migrations:diff
    php app/console doctrine:migrations:migrate

    Pre-requisition to install doctrine migrations and DoctrineMigrationsBundle.

    [doctrine-migrations]
        git=http://github.com/doctrine/migrations.git
    
    [DoctrineMigrationsBundle]
        git=http://github.com/symfony/DoctrineMigrationsBundle.git
        target=/bundles/Symfony/Bundle/DoctrineMigrationsBundle

    Then upgrade vendors to install the new bundles.

    The first command finds out the differences between the entities and the database schema. The second one preform the migration.

    This process will create a tables called “migration_versions”. This table stores the migration version numbers.

  • Create form class with doctrine2

    php app/console generate:doctrine:form [namespace][bundle name]Bundle:[entity name]


    php app/console generate:doctrine:form MooBlogBundle:Comment

    This command create a form type class based on an entity class

  • Details on doctrine2 commands

    php app/console doctrine:schema:create --help

    Adding –help at the end of each command will show you details of all possible options for the current command.

  • Install ACL

    php app/console init:acl

    This commands will create all the needed database tables for ACL to work. More details about ACL can be found in Symfony2 Cookbook
  • Entities from an Existing Database

    Details can be found in Symfony2 cookbook

Related reads

Leave A Reply

Comments

No comments yet, be the first to add one!

Tags