symfony 2 Add values in a collection form in

226 views Asked by At

I'm trying to add values into each element of a collection from a multiple form, but I got this :

Call to a member function getQuestions() on a non-object

What is the correct syntax?

This is my code:

$repository = $this->getDoctrine()->getManager()
                   ->getRepository('***Bundle:Projet');

$projet = $repository->find($projetid);

$formBuilder = $this->createFormBuilder($projet);
$formBuilder->add('questions','collection',array(
        'type' => new QuestionType(),
        'allow_add' => true,
        'allow_delete' => true
));

$form = $formBuilder->getForm();

if ($request->getMethod() == 'POST') {
    $form->bind($request);
    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();

        $membreid = $this->getUser()->getId();

        $questions = $projet->getQuestions();

        //I'm gonna do a loop here
        $questions[0]->setMembreId($membreid);
        $projet->addQuestion($questions[0]);

        $em->persist($projet);
        $em->flush();

        return $this->redirect($this->generateUrl('***_nouveau_projet',array('etape' => 3, 'id' => $projet->getId() )));
        }
}

return $this->render('***:nouveau_projet.html.twig',array(
        'form' => $form->createView(),
            'etape' => $etape,
        'projet' => $projetid,
));
1

There are 1 answers

0
Ahmed Siouani On BEST ANSWER

The error message you got provides relevant clues about what's going wrong.

getQuestions() is called on a non object.

Make sure $project is an instance of ***Bundle:Projet and that

$repository->find($projetid); is returning a valid object.

Probably that the given $projetid doesn't match any record in your date source.