1

under symfony/doctrine, i want to set to the form a value contained in an URL parameter

offer/registration/new?offer_id=23

  public function executeNew(sfWebRequest $request)
  {
    // echo $request->gerParameter('offer_id');
    // echoes 23

    $this->form = $this->configuration->getForm();
    $this->offer_registration = $this->form->getObject();
  }

  public function executeCreate(sfWebRequest $request)
  {
    // echo $request->gerParameter('offer_id');
    // echoes nothing

    $offer = new OfferRegistration();
    $offer->setOfferId($offer_id);
    $this->form = $this->configuration->getForm($offer);
    $this->offer_registration = $this->form->getObject();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  } 

How to pass the $offer_id between executeNew() and executeCreate() so that i can set the right OfferId on the form ?

PS: I precise that i'm working with a admin backend module

j0k
  • 22,600
  • 28
  • 79
  • 90
sf_tristanb
  • 8,725
  • 17
  • 74
  • 118

1 Answers1

2

You would need to pass a parameter when clicking on the NEW link. By default, the symfony generator does not pass any parameters when creating a NEW object.

In order to modify the link that is being generated, you will need to modify the template file that is created. The best way to do this, is to fetch it from your cache folder, and copy it into your /backend/modules/templates folder, and then modify that file.

More information on the admin generator template system can be found here

Once you find the file that generates the Create New link, you can then modify that link to add a parameter.

jgallant
  • 11,143
  • 1
  • 38
  • 72
  • In my case that would be *i guess* : linkToSave($form->getObject(), array( 'params' => array( $offer_id => $request->getParameter('offer_id')), 'class_suffix' => 'save', 'label' => 'Save',)) ?> . Am i right ? – sf_tristanb Feb 02 '11 at 16:16
  • No - The $request object would not be available in your template. You would need to determine what ID you want to pass along to your action. You should have in your array something like: $offer_id => $idVariable --- You could set the variable in the executeIndex action ($this->idVariable = $foo) since the NEW link appears in the Index. – jgallant Feb 02 '11 at 16:18
  • yeah but i already have access to the variable i want in the executeNew. I'm not sure I understood everything here so. – sf_tristanb Feb 02 '11 at 17:51
  • executeIndex fires when displaying your list of items. In your index, it will also display the Create New link. You need to insert the ID in that createNew link. When you click that link, it fires off executeNew .. So essentially, you do not have the variable until after the fact. You will need to figure out how to get that variable from the index instead. – jgallant Feb 02 '11 at 19:33