Symfony 1.4: Add and Update Created_at and Updated_at Fields Automatically
After the CRUD building process with symfony, beginners are often perplexed with the handling of the created_at and updated_at fields on their forms. These fields are typically best handled behind the scenes. Here is how to let symfony take control of these fields automatically.
Symfony is a wonderful framework for getting content developed quickly. However, the downside is sometimes it’s difficult to remember what needs to be built manually and what things need to be edited.
After initially building CRUD, many users get discouraged at something like this:
Nobody really wants to enter created_at and updated_at fields manually. Luckily, symfony will do this for you if you will just get those fields off your form.
class GameForm extends BaseGameForm
public function configure()
Next, you will need to remove this from your form rendering code which is likely found in templates/_form.php. In my example, I need to REMOVE the following code from this file:
<tr> <th><?php echo $form['created_at']->renderLabel() ?></th> <td> <?php echo $form['created_at']->renderError() ?> <?php echo $form['created_at'] ?> </td> </tr> <tr> <th><?php echo $form['updated_at']->renderLabel() ?></th> <td> <?php echo $form['updated_at']->renderError() ?> <?php echo $form['updated_at'] ?> </td> </tr>
Obviously, the more you set away from the generic crud, you need for creating and updated these fields will grow more complex. Hopefully, however, this will get beginners started.
View more articles by David Kirk
Follow the reactions below and share your own thoughts.