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.
/lib/form/doctrine
class GameForm extends BaseGameForm
{
public function configure()
{
unset($this['created_at'], $this['updated_at']);
}
}
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>
Your rendering will obviously change:
However, your those fields which you have removed from your forms will now be correctly updated when you insert or update the object in the datebase.
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.