2012-10-29 29 views
1

我有一个从表单绑定数据到数据库的问题,我有一个表单与测试名称和它的类别,当我想从表单添加值到数据库它只绑定名称测试和类别的只有一个名字,我怎么能对其他类别绑定到数据库...: 我的控制器:绑定从树枝到数据库的许多形式Symfony2

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

    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().' was created'; 
     } 

    else 
    { 
     $success = 'Category name can not be empty'; 
    } 

    return $this->redirect($this->generateUrl('test.new')); 
} 

我的树枝文件:

extends '::base.html.twig' %} 

    {% block stylesheets %} 
    {{ parent() }} 
    <link rel="stylesheet" href="{{ asset('bundles/ladelaodesktester/css/new.css') }}" 
    type="text/css" media="all"> 

<link rel="stylesheet" href="{{ asset('bundles/ladelaodesktester/css/colorpicker.css') 
    }}" type="text/css" media="all"> 
<link rel="stylesheet" href="{{ asset('bundles/ladelaodesktester/css/layout.css') }}" 
    type="text/css" media="all"> 
    {% endblock %} 

    {% block body %} 
    <ul id="breadcrumb"> 
    <li><a href="{{path('homepage')}}" title="Home"><img src="{{ 

    asset('bundles/ladelaodesktester/images/home.png') }}" alt="Home" class="home" /></a> 
    </li> 
    <li><a href="{{path('test.new')}}" title="New"> New test </a></li> 
    <li> Please add data to new test </li> 
    </ul> 
    {#<div class="wrap">#} 
    <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 class="color {valueElement:'myValue'}" type="text" 
     name="category-new"><br> 
     Category 2 <input class="color {valueElement:'myValue1'}" type="text" 
     name="category-new"><br> 
     Category 3 <input class="color {valueElement:'myValue2'}" type="text" 
     name="category-new"><br> 
     Category 4 <input class="color {valueElement:'myValue3'}" type="text" 
     name="category-new"><br> 
     Category 5 <input class="color {valueElement:'myValue4'}" type="text" 
     name="category-new"><br> 


     <input type="submit" value="Add"> 
     </form> 
     </div> 
    <table class="test-list"> 
     <caption></caption> 
     <tbody> 
     <tr> 
      <th>Test name</th> 
     <th># questions</th> 
     <th># passed</th> 
     <th># action</th> 
    </tr> 
    {% for test in tests %} 
    <tr> 
     <td> 
      {{ test.name }} 
     </td> 
     <td> 
      {{ test.countquestions }} 
     </td> 
     <td> 
      {{ test.countresults }} 
     </td> 
     <td> 
      <a href="{{ path('test.show', { 'slug': test.slug }) }}">analyze</a>| 
      <a href="">new result</a> | 
      <a href="{{ path('edit.test', { 'slug': test.slug }) }}">edit </a> 
     </td> 
    </tr> 
    {% endfor %} 
    </tbody> 
    </table> 
    {#</div>#} 
{% endblock %} 
{% block javascripts %} 
    {{ parent() }} 
    <script src="{{ asset('bundles/ladelaodesktester/js/new.js') }}" 
    type="text/javascript"></script> 
<script src="{{ asset('bundles/ladelaodesktester/js/jscolor.js') }}" 
type="text/javascript"></script> 
{% endblock %} 

我类别实体:

class Category 
{ 
/** 
* @var integer $id 
* 
* @ORM\Column(name="id", type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
private $id; 

/** 
* @var string $name 
* 
* @ORM\Column(name="name", type="string", length=255) 
*/ 
private $name; 

/** 
* @var string $color 
* 
* @ORM\Column(name="color", type="string", length=255) 
*/ 
private $color; 

public function __construct() 
{ 
    $this->color = '255,255,0'; // default color for category 
} 

/** 
* Get id 
* 
* @return integer 
*/ 
public function getId() 
{ 
    return $this->id; 
} 

/** 
* Set name 
* 
* @param string $name 
* @return Category 
*/ 
public function setName($name) 
{ 
    $this->name = $name; 

    return $this; 
} 

/** 
* Get name 
* 
* @return string 
*/ 
public function getName() 
{ 
    return $this->name; 
} 

/** 
* Set color 
* 
* @param string $color 
* @return Category 
*/ 
public function setColor($color) 
{ 
    $this->color = $color; 

    return $this; 
} 

/** 
* Get color 
* 
* @return string 
*/ 
public function getColor() 
{ 
    return $this->color; 
} 

} 

回答

0

将输入名称更改为:“category-new []”使其成为数组。

在控制器

$categories = $this->getRequest()->get('category-new'); 
foreach($categories as $category) { 
    $categoryName = new Category(); 
    $categoryName->setName($category); 


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