2012-10-29 2 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(); 
관련 문제