【问题标题】:Multiply categories and bind them to Database Symfony2将类别相乘并将它们绑定到数据库 Symfony2
【发布时间】:2012-10-19 17:14:21
【问题描述】:

我需要帮助我创建的表单,我在一个 twig 文件中创建了 5 个表单并创建了一个控制器,我如何将我的值从表单绑定到数据库,当我现在绑定时它只显示一个值表单5 次,但我需要 5 次才能获得不同的价值 :(,请帮忙,我已经整天坚持这件事了..

我的树枝文件:

<div class="new-test">
     <h2>New test </h2>
     <form action="{{ path('test.create') }}" method="post">

        Test name:   <input type="text" name="name"/><br>
        Category 1<input type="text" name="category-new" >
        <div id="customWidget">
            <div id="colorSelector1"><div style="background-color: #00ff00"></div>  
          </div>
            <div id="colorpickerHolder1"></div>
        </div>

        Category 2<input type="text" name="category-new" ><br>
        Category 3<input type="text" name="category-new" ><br>
        Category 4<input type="text" name="category-new" ><br>
        Category 5<input type="text" name="category-new" ><br>

        <input type="submit" value="Add">
</form>

我的控制器:

 /**
 * @Route("/add/test", requirements={"name" = "\s+"}, name="test.create")
 * @Method("Post")
 * @return array
 */
public function createAction()
{
    $success = 0;
    $name = $this->getRequest()->get('name');




    if( !empty($name) )
    {
        $test = new Test();
        $test->setName($this->getRequest()->get('name'));

        $em = $this->getDoctrine()->getManager();
        $em->persist($test);
        $em->flush();

        $success = 'Test '.$test->getName().' was created';
    }
    else
    {
        $success = 'Test name can not be empty';
    }

      $category = $this->getRequest()->get('category-new');
     for ($i=0; $i<=5; $i++){
     if( !empty($category) )   
       {
        $categoryName = new Category();
        $categoryName->setName($this->getRequest()->get('category-new'));


        $em = $this->getDoctrine()->getManager();
        $em->persist($categoryName);
        $em->flush();

        $success = ' Category '.$categoryName->getName().$i.' was created';

       }

        else
     {
        $success = 'Test name can not be empty';
    }
   }
    return $this->redirect($this->generateUrl('test.new'));
} 

【问题讨论】:

    标签: php forms symfony symfony-2.1


    【解决方案1】:

    不确定我是否理解正确。您希望输入名称为“category-new”且具有不同值的输入。如果是这样,您的表单视图有问题:

    Category *<input type="text" name="category-new" ><br>
    

    name 的末尾应该有括号 name="category-new[]" 或 name="category-new[1]" 用于类别 1,name="category-new[2]" 用于类别 2,依此类推开。

    【讨论】:

      【解决方案2】:

      模板:

          Category 1<input type="text" name="category-new[]" >
          <div id="customWidget">
              <div id="colorSelector1"><div style="background-color: #00ff00"></div>  
            </div>
              <div id="colorpickerHolder1"></div>
          </div>
      
          Category 2<input type="text" name="category-new[]" ><br>
          Category 3<input type="text" name="category-new[]" ><br>
          Category 4<input type="text" name="category-new[]" ><br>
          Category 5<input type="text" name="category-new[]" ><br>
      

      您的控制器采用名称为 category-new 的数组

      /**
       * @Route("/add/test")
       * 
       * @return array
       */
      public function createAction()
      {
          $success = 0;
          $name = $this->getRequest()->get('name');
      
          if (!empty($name)) {
              $test = new Test();
              $test->setName($name);
      
              $em = $this->getDoctrine()->getManager();
              $em->persist($test);
              $em->flush();
      
              $success = 'Test ' . $test->getName() . ' was created';
          } else {
              $success = 'Test name can not be empty';
          }
      
          $categoryList = array_map('trim', $this->getRequest()->get('category-new'));
      
          foreach ($categoryList as $category) {
              if (!empty($category)) {
                  $categoryName = new Category();
                  $categoryName->setName($category);
      
                  $em = $this->getDoctrine()->getManager();
                  $em->persist($categoryName);
                  $em->flush();
      
                  $success = ' Category ' . $category . ' was created';
      
              } else {
                  $success = 'Test category-new may not be empty';
              }
          }
          return $this->redirect($this->generateUrl('test_create'));
      }
      

      一些话

      Route("/add/test", requirements={"name" = "\s+"}, name="test.create")

      您不需要的要求 此操作的名称将为您的 project_bundlename_controller_action {project}_{bundlename}_test_create 我不知道您的项目名称和捆绑名称 - 我认为这对您来说并不困难。还有一些代码:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多