tags:

views:

24

answers:

1

How i can pass values from action class to model class

+1  A: 

Assuming you're not using forms (and therefore you haven't gone through the tutorial on the Symfony site...!), then something like the following would work in your action:

public function executeMyAction(sfWebRequest $request)
{
  if ($request->isMethod("post"))
  {
    $postVar1 = $request->getParameter("postVar1");
    $postVar2 = $request->getParameter("postVar2");

    $model = new MyModel();
    $model->field1 = $postVar1;
    $model->field2 = $postVar2;
    $model->save();
  }
}

Obviously the above includes no data sanitization at all or validation; you'd need to implement this yourself. Use the form framework if possible though; all the validation is handled nicely for you with this, and you can simply pass your request parameters to the form and let it get on with it :-)

richsage
Thanks but iam using form for saving data. $this->form->bind($request->getParameter('question_answers')); if ($this->form->isValid()) { $this->form->question_id=2; $this->form->save(); $this->redirect('@homepage'); }but i want to add an exta field that is not entering on the form
THOmas
In that case, I'd pass in the id as an option to the form and override in your form's doSave(). See http://stackoverflow.com/questions/2461585/passing-values-from-action-class-to-model-class-in-symfony/2461991#2461991
richsage