Symfony 2 – Generating Bundle, Entity and CRUD Module

Hello everybody,
In this article we will explain you the fundaments for generating bundles ( type of file structure ), entity ( class dealing with particular table from the database) and CRUD module ( create, edit, delete, show, list ) for an object in Symfony 2.
One of the best features of Symfony 2 is generating part of the code and in this way we can save a lot of time and energy for programming our websites. The generating process in Symfony 2 is happening based on a custom specification, it is not a completed functionality that we can use everywhere. Symfony 2 is real help for programming middle or large scale web sites and help us to increase the quality of our web development.
If you read the article for Installing and configuring Symfony 2 on our blog, probably you have already installed it and you are working with it. If you didn’t read and you don’t know how to install Symfony 2 Framework we recommend to start with this article first. We think this article is fruitful and helpful for installing Symfony 2 Framework. The generating CRUD module article is like a next step of Symfony 2.
For example we will develop a product module where we can manage our products in our website.
For this example we need to have Symfony 2 framework installed and configured, connected with the database and we also need to have “Product” table. In the “Product” table we insert a few fields: id, name, description, price. We can add field for image, but uploading of an image is a topic that we will cover in another article.
Generating and configuring the bundle structure in Symfony 2 projects.
First, we need to generate the new bundle structure where we will generate the code after that for our module. This can happen when you open Git Bash program, we need to enter in the directory of our project. This we can do with the command:

cd /c/xampp/htdocs/Symfony

Now we can generate our bundle structure in the following way:

php app/console generate:bundle

Symfony 2 – Generating Bundle, Entity and CRUD Module | web development
After typing this command the application will request name of the bundle, where to generate the bundle, format and structure. This process gives us a flexible choice with many options that are very unique feature in Symfony 2. We do have the option to type from the very beginning all the commands to these requests and we can generate the bundle without getting these request one by one during the process. For the purposes of this article we will explain the whole process. After typing the above command we will receive requests like:

Bundle namespace: Mol/StoreBundle

generate-bundle | web development

Bundle name: [MolStoreBundle]:

generate-bundle | web programming

Target directory [C:xampphtdocsSymfony/src]:

Symfony 2 - web programming
This is a standard directory, we just press the enter on keyboard.

Configuration format ( yml, xml, php, or annotation) [annotation]

Symfony 2 – developing of web sites
Here Symfony 2 gives us the opportunity to choose the format of our configuration files. We choose between four types, for this project we choose yml and type it.

Do you want to generate the whole directory structure [no]?

Symfony 2 – developing of web applications
Here we are typing yes because we want the whole structure.

Do you confirm generation [yes]?

press enter.
Symfony 2 – websites programming

Confirm automatic update of your Kernel [yes]?

Symfony 2 – websites development

Confirm automatic update of the Routing [yes]?

generate bundle | Symfony 2
If everything is ok we receive the message:
„You can now start using the generated code!“
Congratulations! The first step from the module is finished. If you open now Symfony/src/Mol/ we will find the bundle file structure StoreBundle, but yet we do not have an entity and CRUD module, we have only the file structure.
Generating classes dealing with particular table in Symfony 2 projects.
Let’s move to step number two: generating the Entity class and the command for that is:

php app/console doctrine:mapping:convert yml ./src/Mol/StoreBundle/Resources/config/doctrine/metadata/orm –-from-database --force

website development
This command creates entities from existing database. Next command is:

php app/console doctrine:mapping:import MolStoreBundle annotation


php app/console doctrine:generate:entities MolStoreBundle

Now, we have generated folder Entity with Product.php file, which contents Product class dealing with Product table from our database.
Generating and configuring CRUD module in Symfony 2 projects.
The third and last step is generating the CRUD module itself. The commands for generating this module are very flexible. The base command is:

php app/console/ generate:doctrine:crud

Symfony 2 – Generating Bundle, Entity and CRUD Module
But we have an additional options like
„—entity=…“, “—route-prefix=…”, “—with-write”, “—format=…”
These fields we can fulfill separately or at once:

php app/console generate:doctrine:crud –-entity=MolStoreBundle:Product -–format=annotation –-with-write –-no-interaction

Then we do expect to receive the messages like:
Generating the CRUD code: OK
Generating the Form code: OK
Then we go src/Mol/StoreBundle/Resources/config/routing.yml and type:

    resource: "@MolStoreBundle/Controller/"
    type: annotation
    prefix: /

Congratulations! Now you completed your module and you can manage your products.
http://localhost/Symfony/web/app_dev.php/product/ This link should work locally and load a list of all products into your website that you have in the table Product at your database. The whole process takes about 5-10 mins. If you have to type it down this by hand it will take you about day or two in the best case or even more. This is one of the differences between Symfony 2 and the other frameworks. Symfony 2 Framework is one of the best frameworks ( even the best one ) because of the principles and ideas that Sensio embodied in it and they are updating the framework often.
Symfony 2 Framework is making the programming process of websites and web applications easy, increase the speed, makes the website cheaper. Symfony 2 is using more for medium and large web applications. Very flexible, fast and very intuitive framework. Symfony 2 is definitely the write choice for us and our websites that we are programming and we strongly recommend Symfony 2 Framework to you. You are going to like it!

Leave a Reply

Your email address will not be published. Required fields are marked *