Symfony2来自一个实体的多个选择标签

时间:2016-03-16 09:24:04

标签: php symfony

Soo,问题是:

我有与实体Group(OneToMany)相关的实体Student

Group1 has Student1 and Student2
Group2 has Student3 and Student4

此外,我还有实体Class,它是表单渲染的主要实体。表单应该解析如下:

<input type="text" name="class[name]"> <-- this one easy

问题从这里开始:(我需要动态渲染这么多select个标签Group实体中有多少记录,其中Student实体记录(相关)在选项中)

<label>Group1</label>
<select name="class[group][0]"> <-- don't not exactly about `name` attr
    <option value="1">Student 1</option>
    <option value="2">Student 2</option>
</select>

<label>Group2</label>
<select name="class[group][student]"> <-- or something
    <option value="3">Student 3</option>
    <option value="4">Student 4</option>
</select>

等等..

我已经了解了自定义字段类型,但可以找到示例如何操作。 并且不知道从哪里开始搜索。

任何帮助都将不胜感激。

更新的

class ClassType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Class'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'class';
    }
}

GroupType

class GroupType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Group'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'b';
    }
}

StundentType

class StudentType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('group')
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Student'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'student';
    }
}

类实体

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

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


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

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

        return $this;
    }

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

集团实体

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

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

    /**
     * @ORM\OneToMany(targetEntity="Student", mappedBy="group")
     */
    private $students;


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

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

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->students = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add students
     *
     * @param \AppBundle\Entity\C $students
     * @return B
     */
    public function addStudent(\AppBundle\Entity\C $students)
    {
        $this->students[] = $students;

        return $this;
    }

    /**
     * Remove students
     *
     * @param \AppBundle\Entity\C $students
     */
    public function removeStudent(\AppBundle\Entity\C $students)
    {
        $this->students->removeElement($students);
    }

    /**
     * Get students
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getStudents()
    {
        return $this->students;
    }
}

学生实体

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

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

    /**
     * @ORM\ManyToOne(targetEntity="Group", inversedBy="students")
     */
    private $group;


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

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

        return $this;
    }

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

    /**
     * Set group
     *
     * @param \AppBundle\Entity\B $group
     * @return C
     */
    public function setGroup(\AppBundle\Entity\B $group = null)
    {
        $this->group = $group;

        return $this;
    }

    /**
     * Get group
     *
     * @return \AppBundle\Entity\B 
     */
    public function getGroup()
    {
        return $this->group;
    }
}

1 个答案:

答案 0 :(得分:0)

您需要根据实体使用formType

http://symfony.com/doc/current/reference/forms/types/form.html

为此目的的选择字段: http://symfony.com/doc/current/reference/forms/types/entity.html

在Symfony控制台中,您可以使用doctrine命令生成formType:

generate
 generate:bundle                          Generates a bundle
 generate:controller                      Generates a controller
 --> generate:doctrine:crud               Generates a CRUD based on a Doctrine entity
 generate:doctrine:entities               Generates entity classes and method stubs from your mapping information
 generate:doctrine:entity                 Generates a new Doctrine entity inside a bundle
 --> generate:doctrine:form               Generates a form type class based on a Doctrine entity

使用生成:doctrine:form 生成一个formType,使用生成:doctrine:crud 生成一个非CRUD控制器,它可以帮助您了解如何使用formType: )

编辑: 导致做的形式: http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#cookbook-form-events-user-data 使用此示例作为基础