The definitive guide of Symfony 1.0

14.1. Code Generation Based on the Model

In a web application, data access operations can be categorized as one of the following:

  • Creation of a record
  • Retrieval of records
  • Update of a record (and modification of its columns)
  • Deletion of a record

These operations are so common that they have a dedicated acronym: CRUD. Many pages can be reduced to one of them. For instance, in a forum application, the list of latest posts is a retrieve operation, and the reply to a post corresponds to a create operation.

The basic actions and templates that implement the CRUD operations for a given table are repeatedly created in web applications. In symfony, the model layer contains enough information to allow generating the CRUD operations code, so as to speed up the early part of the development or the back-end interfaces.

All the code generation tasks based on the model create an entire module, and result from a single call to the symfony command line in the shape of the following:

> symfony <TASK_NAME> <APP_NAME> <MODULE_NAME> <CLASS_NAME>

The code generation tasks are propel-init-crud, propel-generate-crud, and propel-init-admin.

14.1.1. Scaffolding and Administration

During application development, code generation can be used for two distinct purposes:

  • A scaffolding is the basic structure (actions and templates) required to operate CRUD on a given table. The code is minimal, since it is meant to serve as a guideline for further development. It is a starting base that must be adapted to match your logic and presentation requirements. Scaffoldings are mostly used during the development phase, to provide a web access to a database, to build a prototype, or to bootstrap a module primarily based on a table.
  • An administration is a sophisticated interface for data manipulation, dedicated to back-end administration. Administrations differ from scaffoldings because their code is not meant to be modified manually. They can be customized, extended, or assembled through configuration or inheritance. Their presentation is important, and they take advantage of additional features such as sorting, pagination, and filtering. An administration can be created and handed over to the client as a finished part of the software product.

The symfony command line uses the word crud to designate a scaffolding, and admin for an administration.

14.1.2. Initiating or Generating Code

Symfony offers two ways to generate code: either by inheritance (init) or by code generation (generate).

You can initiate a module, that is, create empty classes that inherit from the framework. This masks the PHP code of the actions and the templates to avoid them from being modified. This is useful if your data structure is not final, or if you just need a quick interface to a database to manipulate records. The code executed at runtime is not located in your application, but in the cache. The command-line tasks for this kind of generation start with propel-init-.

Initiated action code is empty. For instance, an initiated article module has actions looking like this:

class articleActions extends autoarticleActions
{
}

On the other hand, you can also generate the code of the actions and the templates so that it can be modified. The resulting module is therefore independent from the classes of the framework, and cannot be altered using configuration files. The command-line tasks for this kind of generation start with propel-generate-.

As the scaffoldings are built to serve as a base for further developments, it is often best to generate a scaffolding. On the other hand, an administration should be easy to update through a change in the configuration, and it should remain usable even if the data model changes. That's why administrations are initiated only.

14.1.3. Example Data Model

Throughout this chapter, the listings will demonstrate the capabilities of the symfony generators based on a simple example, which will remind you of Chapter 8. This is the well-known example of the weblog application, containing two Article and Comment classes. Listing 14-1 shows its schema, illustrated in Figure 14-1.

Listing 14-1 - schema.yml of the Example Weblog Application

propel:
  blog_article:
    _attributes: { phpName: Article }
    id:
    title:       varchar(255)
    content:     longvarchar
    created_at:
  blog_comment:
    _attributes: { phpName: Comment }
    id:
    article_id:
    author:      varchar(255)
    content:     longvarchar
    created_at:
Example data model

Figure 14.1 Example data model

There is no particular rule to follow during the schema creation to allow code generation. Symfony will use the schema as is and interpret its attributes to generate a scaffolding or an administration.

Tip To get the most out of this chapter, you need to actually do the examples. You will get a better understanding of what symfony generates and what can be done with the generated code if you have a view of every step described in the listings. So you are invited to create a data structure such as the one described previously, to create a database with a blog_article and a blog_comment table, and to populate this database with sample data.