如何在Symfony中创建一个复选框标签作为表格?

时间:2016-03-01 10:31:47

标签: php forms symfony

我试图在symfony中创建一个表单,其中有一个复选框标签,但我希望它显示为表格,用户可以在其中查看所有选项的所有详细信息

FormType.php:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name')
        ->add('Age')
        ->add('message', MessageType::class)
        ->add('Mytable', EntityType::class, array('class'=>'EspBundle\Entity\Mytable',
            'multiple'=>true,
            'expanded'=>true,
            'query_builder'=> function(EntityRepository $er){
                return $er->createQueryBuilder('m')->orderBy('m.name','ASC');},
            'choice_label'=>'name',
            ))
        ->add('Save', SubmitType::class)
    ;
}

form.html.twig:

<table>
     {% for item in form.Mytable %}
        {% set id = item.vars['value'] %}
    <tr>
       <td>{{ form_widget(item) }}</td>
       <td>{{ Mytable[id].name }}</td>
       <td>{{ Mytable[id].category}}</td>
       <td>{{ Mytable[id].type }}</td>
        {# and so on ... #}
    </tr>
     {% endfor %}
</table>

但是twig文件给了我以下错误:Variable "Mytable" does not exist in EspBundle:Campaign:form.html.twig,请问您有什么想法吗?如果有人想查看他可以询问的Mytable实体文件。

编辑:

<?php

namespace EspBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;


/**
 * Mytable
 *
 * @ORM\Table(name="mytable")
 * @ORM\Entity
 */
class Mytable
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

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

    /**
     * @var string
     *
     * @ORM\Column(name="category", type="string", length=15, nullable=false)
     */
    private $category;

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

    /**
     * @var string
     *
     * @ORM\Column(name="status", type="string", length=15, nullable=false)
     */
    private $status;

    /**
     * @var string
     *
     * @ORM\Column(name="country", type="string", length=5, nullable=false)
     */
    private $country;


    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="Camp", mappedBy="Mytable")
     */
private $camp;

    /**
     * Constructor
     */
    public function __construct()
    {
        ..
    }


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

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

        return $this;
    }

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

    /**
     * Set isp
     *
     * @param string $category
     * @return Mytable
     */
    public function setCategory($category)
    {
        $this->category= $category;

        return $this;
    }

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

    /**
     * Set type
     *
     * @param string $type
     * @return Mytable
     */
    public function setType($type)
    {
        $this->type = $type;

        return $this;
    }

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

    /**
     * Set status
     *
     * @param string $status
     * @return Mytable
     */
    public function setStatus($status)
    {
        $this->status = $status;

        return $this;
    }

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

    /**
     * Set country
     *
     * @param string $country
     * @return Mytable
     */
    public function setCountry($country)
    {
        $this->country = $country;

        return $this;
    }

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

0 个答案:

没有答案