将类别相乘并将它们绑定到Database Symfony2

时间:2012-10-30 11:10:32

标签: php forms symfony symfony-2.1

我需要帮助我创建的表单,我在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'));
} 

2 个答案:

答案 0 :(得分:1)

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

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

名称的末尾名称应为:“category-new []”或类别1的名称=“category-new [1]”,类别2的名称=“category-new [2]”等等上。

答案 1 :(得分:0)

模板:

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

有些话

  

路由(“/ add / test”,requirements = {“name”=“\ s +”},name =“test.create”)

要求不需要你 这个动作的名称将是project_bundlename_controller_action for you {project} _ {bundlename} _test_create whereis我不知道你的项目名称和包名 - 这不是我想你的困难。还有一些代码:)