Bonjour,

Salut,

J'ai quelques problème pour persister une entité. Cette entité correspond au coté many d'une relation one to many (one <gras>Candidate </gras>--> many <gras>Interview</gras>).
J'ai donc un formulaire avec l'ID de mon candidat (entité <gras>Candidate</gras>) et en dessous j'ajoute autant d'interview que nécessaire.

Voici le code de mon contrôleur:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php 	public function saveinterviewAction($id)
	{    
 
		$request=$this->getRequest();
		$em = $this->getDoctrine()->getEntityManager(); 
		$entity = $em->getRepository('AdlHiringBundle:Candidate')->find($id);
		$form = $this->createForm(new CandidateTypeInterview(), $entity);
 
 
        $form->bindRequest($request);
        $em->persist($entity);
		foreach($entity->getInterviews() as $interview) 
            {
				$em->persist($interview);
            }
        $em->flush();
 
		return $this->render('AdlHiringBundle:Candidate:saveinterview.html.twig', array(
		    'entity' => $entity,
 
 
        ));
 
	}
CandidateTypeInterview
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
 
namespace Adl\HiringBundle\Form;
 
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
 
 
class CandidateTypeInterview extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
 
            ->add('FIRSTNAME')
			->add('interviews', 'collection', array('type' => new InterviewType,'allow_add' => true))
        ;
    }
 
    public function getName()
    {
        return 'adl_hiringbundle_candidatetype';
    }
 
	public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'Adl\HiringBundle\Entity\Candidate',
			'csrf_protection' => true,
            'csrf_field_name' => '_token',
            'intention' => 'candidate_item',    // a unique key to help generate the secret token
        );
    }
 
}
InterviewType

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
 
namespace Adl\HiringBundle\Form;
 
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
 
class InterviewType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
 
			->add('COMMENT')
 
 
        ;
    }
 
    public function getName()
    {
        return 'adl_hiringbundle_interviewtype';
    }
 
	public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'Adl\HiringBundle\Entity\Interview',
			'csrf_protection' => true,
            'csrf_field_name' => '_token',
            'intention' => 'interview_item',    // a unique key to help generate the secret token
        );
    }
 
}
Mon problème : impossible de persister l'entité Interview. Une idée ?