php - Symfony2 multiple select tags from one entity

250

Soo, the problem would be following:

I have entityGroup in relation with entityStudent (OneToMany).

Group1 has Student1 and Student2
Group2 has Student3 and Student4

Also I have entityClass which is the main one for form to render. Form should be parsed something like this:

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

Problem begins here: (I need to render dynamically so manyselect tags how many there is records inGroup entity withStudent entity records (related) in options)

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

And so on..

I've read about custom field types, but could find example how to do it. And have no idea where to begin searching.

Any help would be appreciated.

UPDATED

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 entity

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

Group entity

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

Student entity

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

Answer

Solution:

You need to use formType based on your entities

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

Choice field for this purpose : http://symfony.com/doc/current/reference/forms/types/entity.html

In the Symfony console you can generate the formType with doctrine command :

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

Generate a formType with generate:doctrine:form and un CRUD controller with generate:doctrine:crud it's help you to see how to use formType :)

EDIT : lead to do the form : http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#cookbook-form-events-user-data Using this example as base

People are also looking for solutions to the problem: mysql - PHP undefined index / variable

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.