Compose a MVC Paradigm for PHP with Symfony - Page 2

Configure the Database for Bookshop

To develop the database for the bookshop application, start by creating an empty database under MySQL RDBMS named symfonydb. When you have done that, you then need to indicate to Symfony (and mandate to bookshop) to use this database. For this, you need to execute the configure:database task, like this:

Note: The configure:database task takes three arguments: the PDO DSN, the username, and the password (optional) to access the database.Note: The Symfony framework supports all PDO-supported databases, like MySQL, PostgreSQL, SQLite, Oracle, MSSQL, and so on.Note: The configure:database task stores the database configuration into the /config/databases.yml configuration file. Instead of using the task, you can edit this file by hand.

At this point, Symfony knows which database to use, and you are ready to develop the relational model. Your database will contain a single table, named books, that has the following diagram:

The road from this diagram to the database is facilitated by the Symfony-supported ORMs (Propel or Doctrine). The ORM needs a description of the tables and their relationships to create the related classes. The two ways to create this description schema are introspecting an existing database and creating it by hand.

Obviously, in this case, you can do it by hand. Edit the empty config/schema.yml file, like this:

Now, based on the schema.yml, you can generate the proper SQL statements by running the propel:build-sql task, like this (the statements will be stored in the data/sql/ directory, optimized for the database engine you have configured, MySQL):

The ORM also generates PHP classes that map table records to objects. You can accomplish this by running the propel:build-model task, like this (the generated PHP files are stored in lib/model/ folder):

The tables have been created in the database, but there is no data in them. Each time Symfony creates tables in the database, all the data are lost. To populate the database with some initial data, create YAML files in the data/fixtures/ directory and use the propel:data-load task to load them into the database.

Generate the Web Pages

Now you have everything you need to create web pages that interact with the symfonydb database. The idea is to generate an application that displays a list of books, and allows the user to edit an existing book and delete a book. A Symfony project is composed of applications, and each application is further divided into modules. A module is a self-contained set of PHP code that represents a feature of the application, or a set of manipulations the user can do on a model object (a book module, for example). Symfony can automatically generate a module for a given model that provides basic manipulation features. To enable this feature, run the propel:generate-module task, like this (you generate the book module for the books model):

Customize Web Pages

To customize the generated web page, you first must know that in Symfony the content is rendered by a global template called a layout. The default layout of an application is called layout.php and you can find it in the apps/frontend/templates/ directory. This directory contains all the global templates for an application.

Note: The stylesheets, images, JavaScript and uploads are stored by default under the /web folder (in /css, /images, /js, and /uploads folders, respectively). Therefore, you should place your images under the /images folder.

Now, you can test the application again at http://localhost/frontend_dev.php/book. You should see something similar to Figure 6.

Bookshop Complete

You have now completed the mechanism for seeing the books, the function for deleting/editing/adding a book, and the view for searching for a book. As you saw, Symfony did most of the work. Symfony is capable of so much more, but a full examination of the framework is well beyond the scope of this article. I hope that you have witnessed enough of Symfony's power to explore what else it can do at symfony-project.org.

About the Author

Octavia Andreea Anghel is a senior PHP developer currently working as a primary trainer for programming teams that participate at national and international software-development contests. She consults on developing educational projects at a national level. She is a coauthor of the book "XML Technologies--XML in Java" (Albastra, ISBN 978-973-650-210-1), for which she wrote the XML portions. In addition to PHP and XML, she's interested in software architecture, web services, UML, and high-performance unit tests. to e-mail her.