Developing with Symfony1.2, NetBeans6.

8, PostgreSQL in WampServer – Part 2
(a) Creating a database application using Symfony and NetBeans
1. If you have gone through Part 1 of this tutorial then proceed further otherwise please go to
Part 1 of this tutorial i.e. ‘Developing with Symfony1.2 and NetBeans6.8 in WampServer Part 1’, as you need to install and configure symfony and NetBeans, that’s given in Part 1.

2. Start NetBeans by clicking and Click on File  New Project. Creation of this project start
with following screen. Please click on PHP, PHP Application and Next button.

3. Next screen will ask for Project Name give it myfirstproject. I am developing same
program which in given as My First Project on Symfony’s site i.e. You can ready project from there also. Please note down Sources Folder it must be Document Root of your WampServer given as shown below:

4. Next screen will tell you about Run Configuration as following:

5. On next screen, please select Symfony PHP Web framework and give –csrf-secret as I have
given ‘nvne09kmr10sam1976’. Click on Finish button.

6. This will create a lot of directories as follows:

7. Please click on myfirstproject and press ‘Alt+L’. This will give you symfony command box.
Please type configure in Filter box. Click on configure:database in Matching Tasks and give “pgsql:dbname=myfirstproject;host=localhost” postgres postgres in Parameters. In above string: a. pgsql – is driver to PostgreSQL b. myfirstproject – is database you have in your postgresql c. host – host on which you are creating d. postgres – is username e. postgres – is password You should create this database named as myfirstproject by opening pgAdmin III. The screen for configure:database will look like as following:

8. Above command will change your /myfirstproject/config/databases.yml Look at your dsn,
username and password will changed according to configure:database command. databases.yml will look like as following:

9. Now open /myfirstproject/config/schema.yml and create schema of your project as
following. Please care of indentation and don’t use tab key while creating schema. First you have to write connection name i.e. propel, then table names i.e. blog_post and blog_comment further they hold fieldnames like blogpost has id, title, excerpt, body, created_at

10. Now schema is ready. Please create model from it. Press ‘Alt+L’ to invoke symfony command box and run command: propel:build-model

11. Above command will create a folder model in /myfirstproject/lib folder of project directory.
In model folder one each table has two classes one is object class this will hold get and set methods and other is peer class this class includes database operations (i.e. insert, select, delete). The files in /myfirstproject/lib/model are blank, these files are for you, you can write your customized code in these files. Take a look at created directories as below:

12. Now model is ready, you can create SQL from it by using propel:build-sql Please press
‘Alt+L’ and give propel:build-sql command as following:

13. Above command will create a file lib.model.schema.sql in /myfirstproject/data/sql which
holds sql commands to create tables and set primary and foreign key given following:

14. SQL commands created in previous step will be executed by propel:insert-sql Please press
‘Alt+L’ and give command. NetBeans will ask for confirmation message. Please press ‘Y’ there.

15. Above command will create two tables i.e. blog_comment and blog_post in your
PostgreSQL. Please open pgAdmin III and see the tables in myfirstproject. It must be as following:


Now your database and model is ready. Create forms using propel:build-forms. Please press ‘Alt+L’ and give propel:build-forms as following:


Above command will create form folder in /myfirstproject/lib folder of project. As we have discussed in Model creation each table create two forms one in base folder and other outside base folder in form folder. You can customize form outside base folder according to your requirement. Avoid customization of the forms in base folder because it will got changed every time you execute propel:build-forms


Until now forms are ready. Now for database operation like Create, Read, Update, Delete you have to create modules. Symfony has commands for that also, it will create your CRUD operation automatically for you. Use following commands propel:generate-module frontend post BlogPost propel:generate-module frontend comment BlogComment Please press ‘Alt+C’ to clear cache.


Above two commands will create two modules (i.e. two directories) in /myfirstproject/apps/frontend/modules as following:


Each directory here contains two folders one is actions and other is templates. Actions folder contains Controller and templates contain View of the module. Two folders in post are shown as following:


Now the project is ready, we have to setup web server. Please create an alias of myfirstproject by clicking on WampServer Icon  Apache  Alias directories  Add an alias


Give alias name myfirstproject, give pathname C:\wamp\www\myfirstproject/web. The screens for these steps are as following:



Now web server is also ready. Open your browser and give url as: http://localhost/myfirstproject/frontend_dev.php Output must be as following:


Now give this url: http://localhost/myfirstproject/frontend_dev.php/post. You will see it is displaying contents of your blog_post table.


If you look at url we are using frontend_dev.php in it. What is it? It is front controller to the development environment of Symfony. Symfony supports different environments for different purposes:

• • • 26.

The development environment: This is the environment used by web developers when they work on the application to add new features, fix bugs etc. (frontend_dev.php) The test environment: This environment is used to automatically test the application. The production environment: This is the environment end users interact with. (index.php)

In step 24, you are giving http://localhost/myfirstproject/frontend_dev.php/post. By default when we give a url we give it as modulename/actionname.but in above url you are only giving module name i.e. post. When you do this Symfony will automatically take it as post/index that means it is invoking index action of post module. It will go to controller (i.e. /myfirstproject/apps/frontend/modules/post/actions/actions.class.php) of the post module and search for executeIndex() function.


As mentioned in above step in executeIndex() function, it will put the contents of blog_post table in blog_post_list array and give the control to indexSuccess.php template to display contents of blog_post_list. The indexSuccess.php will look like this:


Anyway open browser and give url as http://localhost/myfirstproject/frontend_dev.php/post Click on New link, Fill the form as following screen and click on Save button to save the contents of form.


After that click on Cancel to see the saved contents. The screen will look like as following:


In next step. Please open second url as: http://localhost/myfirstproject/frontend_dev.php/comment Now click on New link. You will get an error like as following:

31. Above step is giving error because comment table will hold foreign key to blog_post. We
have not given the field for blog_post to return in blog_comment. For this please add a magic method to BlogPost as following:

32. Refresh the page in step 30, You will get the comment form as following:

33. Save the content and Click Cancel to see the saved contents. 34. Now its time to format the contents of the project. Please open /myfirstproject/web/css/main.css and add some css classes as following into it:

35. Now again open browser and give url as:
http://localhost/myfirstproject/frontend_dev.php/comment You can see the effect of css classes on next screen:

36. You can change the layout of the project with single file. Please open
/myfirstproject/apps/frontend/templates/layout.php and change the body part of layout.php as following:

37. Open the url given in step 35 again in browser and see the effect. The screen will look like as following:

38. Now its time to create a starting module. Please create a new module by pressing ‘Alt+L’ and
give command: generate:module frontend main

39. Please delete single line in executeIndex() of
/myfirstproject/apps/frontend/modules/main/actions/actions.class.php. The screen will look like as following:

40. Please change indexSuccess.php of
/myfirstproject/apps/frontend/modules/main/templates to give a welcome message as following:

41. Now set your starting module by editing /myfirstproject/apps/frontend/config/routing.yml
Change homepage as module: main The file will look like as following:


42. Please clear cache by pressing ‘Alt+C’. Your application is ready, see it with browsing
following url: http://localhost/myfirstproject/frontend_dev.php/main

You can navigate to List of Posts and List of Comments

43. Now project is ready. Its time to package the project for deployment. To package the project
use following command: project:freeze C:\wamp\www\symfony-1.2.10\data\bin The screen for above command is below:

44. The freeze command will add three folders i.e. /myfirstproject/data/symfony,
/myfirstproject/lib/symfony and /myfirstproject/web/sf into project as shown below. With this command you are adding symfony library files to your project. Now your project folder is ready to deploy.

45. That’s all the application is finished. With this tutorial I want to tell you about how to
develop a basic application using Symfony and NetBeans. For detailed study you have to go through Symfony book (, Getting Started (, My First Project ( and Forms in Action ( given on Symfony website.

Sign up to vote on this title
UsefulNot useful