This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

Retrieve item ID from a DropDownList

Discuss Symfony 2 bundles here

Retrieve item ID from a DropDownList

by siva_dashq » Wed Dec 10, 2014 5:53 pm

Hello guys,

I've been working on Symfony for a couple of days, and i'm currently keeping up a co-worker app.

Basically, i want to be able to retrieve the ID of an item in a drop down list.
I have a variable that is an array, the result of a "SELECT * FROM table" kind of query. It's an array of an entity called Question and I want to put all the results in a drop down list form and retrieve user's choice within this list by it's id.

You'll understand a bit better if i show you some code :

So here is a (relevant) part of the creation form :
Code: Select all
private $choices;
    public function __construct(array $questions)
    {
        $this->choices = array();
        foreach ($questions as $question)
        {
            $this->choices[$question->getId()] = $question->getQuestionGroup()->getQuestionnaire()->getName() . ' | ' . $question->getTitle();
        }
    }
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('question', 'choice', array(
                'label'   => 'question_group.form.questions',
                'choices' => $this->choices,
            ))
        ;
    }


And from my controller i want to retrieve the selected entity Question :
Code: Select all
public function duplicateAction(Request $request, $projectId, $questionGroupId)
    {
        $em = $this->getDoctrine()->getManager();
        $questions = $em->getRepository('AppBundle:Question')->getAllWithQuestionnaire();
        $form = $this->createForm(new QuestionDuplicateType($questions));
        $form->handleRequest($request);
        if ($form->isValid())
        {
            $data = $form->getAttribute('question');
            /* I HAVENT DONE THE REST YET */
            return $this->redirect($this->generateUrl('question_edit', ['projectId' => $projectId, 'questionGroupId' => $questionGroupId /*, id question ]));
        }
       
        return $this->render(
            'Question/duplicate.html.twig',
            [
                'projectId' => $projectId,
                'form'      => $form->createView(),
            ]
        );
    }


So. does $data contains the id of the selected item here? If not, how can i do that?


Thanks
siva_dashq
Junior Member
 
Posts: 1
Joined: Wed Dec 10, 2014 5:30 pm