2

I'm creating my own blog engine to learn Symfony, and I have a question :

In the generated administration pages for a blog post, I have a drop-down list of authors, to indicate the author_id.

I'd like to hide that drop-down list, and set the author_id to the id of the current logged-in user when the post is created (but not when it is edited)

How can I accomplish that ?

Edit I've tried those :

$request->setParameter(sprintf("%s[%s]", $this->form->getName(), "author_id"), $this->getUser()->getAttribute("user_id"));
$request->setParameter("content[author_id]", $this->getUser()->getAttribute("user_id"));
$request->setParameter("author_id", $this->getUser()->getAttribute("user_id"));
$request->setParameter("author_id", 2);
$request->setParameter("content[author_id]", 2);
$request->setParameter("author_id", "2");
$request->setParameter("content[author_id]", "2");

In processForm() and executeCreate()

Resolved !

The final code is :

  public function executeCreate(sfWebRequest $request)
  {
    $form = $this->configuration->getForm();
    $params = $request->getParameter($form->getName());
    $params["author_id"] = $this->getUser()->getGuardUser()->getId();;
    $request->setParameter($form->getName(), $params);

    parent::executeCreate($request);

  }
Manu
  • 4,410
  • 6
  • 43
  • 77
  • I can't believe how hard it is to find info on this. Am I the first one to want to save the author of document ? – Manu Jan 21 '11 at 17:37

3 Answers3

2

Override the executeCreate function in the actions file. When binding post data to the form, merge the current user's id into it.

2nd update

I did some experimenting, and this works:

class fooActions extends autoFooActions
{
  public function executeCreate(sfWebRequest $request)
  {
    $form = $this->configuration->getForm();
    $params = $request->getParameter($form->getName());
    $params["author_id"] = 123;
    $request->setParameter($form->getName(), $params);

    parent::executeCreate($request);
  }
}
Maerlyn
  • 33,687
  • 18
  • 94
  • 85
  • This seems like a good idea; could you detail it a little more ? – Manu Jan 20 '11 at 23:24
  • I'll try http://stackoverflow.com/questions/4727314/symfony-admin-generator-doctrine-executecreate – Manu Jan 21 '11 at 16:56
  • thanks; it still fails to save, saying that author_id is required .. I'll try to make it optional. – Manu Jan 22 '11 at 11:24
  • Make sure your form field's value is sent as `formname[author_id]`, that's the default format, so I assumed it in my code. – Maerlyn Jan 22 '11 at 12:05
  • It seems that the new executeCreate is not called ... I've added "$i = 3/0;" and it didn't throw an error. – Manu Jan 22 '11 at 14:32
  • Create is the action that creates the new object, it should be called. I can't give you tips how to debug this one, never seen it not getting called. Maybe check the form's action attribute to see where it posts the data. – Maerlyn Jan 22 '11 at 14:51
  • Nevermind, it is called. It throws an error if I write $this->foo; – Manu Jan 23 '11 at 07:12
0

change the widget in the form with the sfWidgetFormInputHidden and set the value with sfUser attribute (that defined when a user logged in)

override the executeCreate() and set the author_id widget (thanks to maerlyn :D )

public function executeCreate(sfWebRequest $request){
  parent::executeCreate($request);
    $this->form->setWidget('author_id', new sfWidgetFormInputHidden(array(),array('value'=>$this->getUser()->getAttribute('author_id'))) );
}
HQM
  • 558
  • 1
  • 3
  • 15
  • 2
    Using `sfContext::getInstance()` is bad practise. Use dependency injection. Suggested reading: http://webmozarts.com/2009/07/01/why-sfcontextgetinstance-is-bad/ – Maerlyn Jan 20 '11 at 06:23
  • As Maerlyn suggests using context is a bad practice. Just pass author_id into the form as an option. – Jakub Zalas Jan 20 '11 at 21:18
  • but the rest is valid ? $this->setWidget('author_id', new sfWidgetFormInputHidden(array(),array('value'=>1))) is ok ? – Manu Jan 20 '11 at 22:49
  • it is valid, but as mentioned by Maerlyn it's highly not recommended .. :D – HQM Jan 21 '11 at 03:08
  • $this->form->setWidget AFTER parent::executeCreate ?? – Manu Jan 21 '11 at 16:38
  • Using a hidden input is absolutely unreliable. Your user will be able to post as any other user, not just himself. Also: `parent::executeCreate` will save the object and redirect to the index action, code after that is not executed. – Maerlyn Jan 21 '11 at 18:03
  • oo yeah .. i've forgot about the processForm in the parent::executeCreate :D and redirect to the edit action – HQM Jan 22 '11 at 00:41
0

In Objects , the solution is: (new and $this)

class fooActions extends autoFooActions
{
  public function executeCreate(sfWebRequest $request)
  {
    $this->form = new XxxxxForm();
    $params = $request->getParameter($this->form->getName());
    $params["author_id"] = 123;
    $request->setParameter($this->form->getName(), $params);

    parent::executeCreate($request);
  }
}
Greg
  • 23,155
  • 11
  • 57
  • 79
Fernando
  • 1,126
  • 12
  • 13