1

i have problem with binding data from a form to database, I have a form with test name and it's categories, when i want to add values from forms to database it only binds the name of the test and only ONE name of the category, how can i make to bind other categories to database ...: My controller:

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

My twig file:

 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 %}

my entity of category:

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

}
4

1 回答 1

0

Change the input name to: "category-new[]" to make it an array.

in the controller:

$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();
于 2012-10-31T21:58:59.600 回答