Symfony1 如何使用symfony在数据库中存储值?

Symfony1 如何使用symfony在数据库中存储值?,symfony1,symfony-1.4,Symfony1,Symfony 1.4,我是symfony的新手,在经历了几个在线教程和博客之后,我正在尝试创建一个简单的项目。我用命令控制台创建了一些模块。现在我已经使用简单的html创建了一个表单,现在我需要将表单值存储在数据库中。我试着用一种简单的方式来做,就像我得到了一个sfWebRequest$request数组,然后我做了如下事情:- $name = $request->getPostParameters()['name']; $email = $request->getPostParameters()['em

我是symfony的新手,在经历了几个在线教程和博客之后,我正在尝试创建一个简单的项目。我用命令控制台创建了一些模块。现在我已经使用简单的html创建了一个表单,现在我需要将表单值存储在数据库中。我试着用一种简单的方式来做,就像我得到了一个
sfWebRequest$request
数组,然后我做了如下事情:-

$name = $request->getPostParameters()['name'];
$email = $request->getPostParameters()['email'];
$password = $request->getPostParameters()['password'];
然而,我完全能够在数据库中存储字段,但我对我的方式有点困惑。有没有其他更好的方法可以做到这一点。如果我有一个表单,有十个字段,我想把它们存储在两个表中,怎么能做到呢

下面是我的HTML代码

 <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="info@tympanus.net" type="email" AUTOCOMPLETE=OFF />
                        </p>
                        <p>
                            <label for="password">Password</label>
                            <input id="password" name="password" type="password" AUTOCOMPLETE=OFF />
                        </p>
       ......
       ......

您应该使用
sfForm
来处理表单验证。如果您在模型中使用条令,那么就更容易了,因为您可以基于模式定义生成所有的基础

无论何时使用
sfForm
您的操作都将如下所示:

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);
      }
   }
}

你的问题不清楚你是否在使用ORM

你们说“我完全能够在数据库中存储字段”,但我不确定你们目前是怎么做的

@prodigitalson就如何创建表单并将这些字段保存到数据库中给了您一个很好的解释,但我想补充一点,您首先需要有一个模型才能工作

如果您没有,请查看:

对于您的问题:“如何使用symfony在数据库中存储值?”的直接答案是:

1) 为模型定义架构(在schema.xml或schema.yml中) 2) 建设;它将根据您的模式创建表。在推进的情况下,它是./symfony推进:构建所有负载 3) 这个构建过程将生成prodigitalson所说的表单类(扩展sfForm)。 4) 照神童的建议去做

更多信息请访问:

您也可以不使用sfForms来完成。您的操作将类似于:

(非常简单的例子):


希望这能有所帮助。

感谢您的回复,我正在使用symfony 1.4,我有一个jquery表单,希望提交并存储其中的字段database@showket:好的,那么你是使用条令、推进还是不使用ORM?你在使用sfForm吗?我这里没有使用sfForm,我嵌入了一个与sfForm无关的jquery表单。Shell我用html代码将问题更新为well@showket:是,请更新。您不使用sfForm有什么原因吗?@prodigitalson非常感谢先生,我对symfony很陌生,很抱歉问了这么多烦人的问题Fyi,检索请求参数的首选方法是$request->getParameter('password');如果指定名称的请求参数不存在,您还可以传递要返回的“dafault”第二个参数。您可能会发现Jobeet days并感兴趣。我认为您可以从Jobeet开始学习symfony 1.4。
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);
      }
   }
}
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

    }

}