Q:

Symfony: How to hide form fields from display and then set values for them in the action class

I am fairly new to symfony and I have 2 fields relating to my table "Pages"; created_by and updated_by. These are related to the users table (sfGuardUser) as foreign keys. I want these to be hidden from the edit/new forms so I have set up the generator.yml file to not display these fields:
form:
  display:
    General: [name, template_id]
    Meta: [meta_title, meta_description, meta_keywords]

Now I need to set the fields on the save. I have been searching for how to do this all day and tried a hundred methods. The method I have got working is this, in the actions class:
protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form_params = $request->getParameter($form->getName());
    $form_params['updated_by'] = $this->getUser()->getGuardUser()->getId();
    if ($form->getObject()->isNew()) $form_params['created_by'] = $this->getUser()->getGuardUser()->getId();

    $form->bind($form_params, $request->getFiles($form->getName()));

So this works. But I get the feeling that ideally I shouldnt be modifying the web request, but instead modifying the form/object directly. However I havent had any success with things like:
$form->getObject()->setUpdatedBy($this->getUser()->getGuardUser());

If anyone could offer any advice on the best ways about solving this type of problem I would be very grateful.
Thanks,
Tom 

A:

After processing and saving the form you could set those fields on the object and re-save:
protected function processForm(sfWebRequest $request, sfForm $form)
{
  $form->bind($request->getParameter($form->getName()));

  if ($form->isValid())
  {
    $page = $form->save();
    $user = $this->getUser()->getGuardUser();
    $page->setUpdatedBy($user);
    if (empty($page->created_by))
    {
      $page->setCreatedBy($user);
    }

    $page->save();

    $this->getUser()->setFlash('notice', 'Successfully saved page.');
    $this->redirect('@homepage');
  }
}

There's also a Doctrine extension called Blameable that automatically sets edited_by and created_by fields on specified models. The Doctrine website is undergoing some reorganization but here is the cached page for the extension.

