views:

153

answers:

2

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: 

To process your form create a new object, set the fields then save.

$article = new Article();
$article->setName($request->getParameter($form->getName());
$article->setDescription($request->getParameter($form->getDescription());
$article->setMetaKeywords($request->getParameter($form->getMetaKeywords());
$article->save();
Jon Winstanley
Hi Jon, Thanks for your answer! However this doesnt look like its using any of the sf core functionality. $form->bind() should (and does) automatically handle all of the submitted fields. $form is already related to the object (Article in your example) by $form->getObject() (right?) so that should make the last line in my post valid...? But it isnt!!
Tom
That's true, to be honest, I have only recently moved to Symfony 1.3 from 1.0 and so I may be doing things the *old* way.
Jon Winstanley
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.

gruner
Blameable is exactly what I need, thanks a million - and including the cached page too - legend.Incidentally though, your solution wouldnt quite have worked for me since the created_by/updated_by fields are foreign keys and $form->save() wont let the null values through.
Tom