0
votes

I am new to symfony, after going through several online tutorials and blogs, i am trying to create a simple project. I created some modules with command console. and now i have created a form using simple html and now i need to store the form values in database. I am trying to do it in a simple way, like i am getting an array of sfWebRequest $request and then i did something like:-

$name = $request->getPostParameters()['name'];
$email = $request->getPostParameters()['email'];
$password = $request->getPostParameters()['password'];

however I am perfectly able to store fields in database, but i am a bit confused about the way i am doing. is there any other better way to do the same. and if i have a single form with ten fields, and i want to store them in 2 tables, how can do that.

HERE GOES MY HTML CODE

 <form id="formElem" name="formElem" action="/register/register" method="post">
                    <fieldset class="step">
                        <legend>Account</legend>
                        <p>
                            <label for="username">User name</label>
                            <input id="username" name="username" />
                        </p>
                        <p>
                            <label for="email">Email</label>
                            <input id="email" name="email" placeholder="[email protected]" type="email" AUTOCOMPLETE=OFF />
                        </p>
                        <p>
                            <label for="password">Password</label>
                            <input id="password" name="password" type="password" AUTOCOMPLETE=OFF />
                        </p>
       ......
       ......

And here is my Action Function:-

public function executeRegister(sfWebRequest $request)
     {
       if ($request->isMethod('post'))
     echo $request->getParameter('password');exit;

         $this->redirect('user/login');
      }
2
FYI, the preferred way to retrieve request parameters is $request->getParameter( 'password' ); You can also pass a "dafault" second parameter to be returned if a request parameter by the name specified does not exist.0x6A75616E
You might find Jobeet days 3 and 10 to be of interest.user212218
I think you can start learn symfony 1.4 from Jobeet.denys281

2 Answers

1
votes

You should use sfForm to handle form validation. If you use Doctrine for your models then it will be even easier because you can generate all the basics based on the schema definition.

Regardless when using sfForm your action would look something like the following:

public function executeSave(sfRequest $request)
{
   $this->form = new MyFormThatExtendsSfForm();
   if($request->isMethod('post')){
      $this->form->bind($request->getParameter($this->form->getName()));
      if($this->form->isValid()){
         // the array of values passed in the form
         $values = $this->form->getValues();

         // do your save logic here
         // if its a doctrine form this logic will look simply like
         // $this->form->save();

         // redirect to your success action
         $this->redirect($successUrl);
      }
   }
}
1
votes

It's not clear from your question whether you're using an ORM or not.

You say that "I am perfectly able to store fields in database", but I am not sure how you're doing it currently.

@prodigitalson gave you a good explanation on how to create a form and get those fields to save to your database, but I wanted to add that you need to have a model for that to work first.

In case you don't, please take a look at:

http://www.symfony-project.org/jobeet/1_4/Propel/en/03

The direct answer to your question: "how would i store values in database using symfony?" is:

1) Define a schema for your model (within schema.xml or schema.yml) 2) Build; which will create your tables based on your schema. In propel's case, it's ./symfony propel:build-all-load 3) This build process will generate the Form class (that extends sfForm) that prodigitalson is talking about. 4) Do what prodigitalson suggested.

More on that is available here: http://www.symfony-project.org/forms/1_4/en/

You could also do it without using sfForms. Your action would look something like:

(very simplistic example):

public function executeSaveUser( sfWebRequest $request ){

  $full_name        = $request->getParameter( 'full_name', null );
  $email_address    = $request->getParameter( 'email_address', null );

  // Validate your input here.. e.g. make sure email is valid etc.

  // Create a new User 
    try{

        $user = new User();

        $user->setFullName( $full_name );
        $user->setEmailAddress( $email_address ) ;

        $user->save();

    }catch( Exception $e ){

        // handle error response here..

    }

  // Respond with JSON  
    if( !$request->isXmlHttpRequest() ){ // if this is an ajax call

        $this->setLayout(false);
        $this->getResponse()->setContentType('application/json');

        $response = array( 'user_id' => $user->getId() );

        $this->renderText( json_encode($response) );

        return sfView::NONE;

    } else {

        return sfView::SUCCESS; // display your template

    }

}

Hope that helps.