Ich möchte eine ManyToMany Beziehung mit Symfony3/Lehre schaffen (Die Einheiten sind ‚Kategorie‘ und ‚Service‘)Relation ManyToMany mit Lehre/symfony3
So habe ich zwei Formen dieses entites zu erstellen.
Die erste Form (Kategorie) funktioniert aber nicht der zweite (Service): Der neue Service ist nicht auf Kategorien beziehen, ich verstehe nicht:
Categorie.php
/**
* Categorie
*
* @ORM\Table(name="categorie")
* @ORM\Entity(repositoryClass="GestionBundle\Repository\CategorieRepository")
*/
class Categorie
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToMany(targetEntity="Service", inversedBy="categories")
*/
private $services;
/**
* Categorie constructor.
*/
public function __construct()
{
$this->services = new ArrayCollection();
}
[...]
}
-Service .php
/**
* Service
*
* @ORM\Table(name="service")
* @ORM\Entity(repositoryClass="GestionBundle\Repository\ServiceRepository")
*/
class Service
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToMany(targetEntity="Categorie", mappedBy="services")
*/
private $categories;
/**
* @var string
*
* @ORM\Column(name="nom", type="string", length=255, unique=true)
*/
private $nom;
/**
* Categorie constructor.
*/
public function __construct()
{
$this->categories = new ArrayCollection();
}
[...]
}
CategorieType.php
class CategorieType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nom')
->add('services', EntityType::class, array(
'class' => 'GestionBundle:Service',
'choice_label' => 'nom',
'multiple' => true,
'required' => false))
->add('Ajouter', SubmitType::class)
;
}
[...]
}
ServiceType.php
class ServiceType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nom')
->add('categories', EntityType::class, array(
'class' => 'GestionBundle:Categorie',
'choice_label' => 'nom',
'multiple' => true,
'required' => false))
->add('Ajouter', SubmitType::class)
;
}
[...]
}
Controller:
/**
* @Route("/Categories/Creation", name="route_gestion_eltcoord_categories_creation")
* @Template()
*/
public function CreationCategorieAction(Request $request)
{
$Categorie = new Categorie();
$form = $this->createForm(CategorieType::class, $Categorie);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($Categorie);
$em->flush();
return $this->redirectToRoute('route_gestion_eltcoord_categories');
}
return array('form' => $form->createView());
}
/**
* @Route("/Services/Creation", name="route_gestion_eltcoord_services_creation")
* @Template()
*/
public function CreationServiceAction(Request $request)
{
$Service = new Service();
$form = $this->createForm(ServiceType::class, $Service);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($Service);
$em->flush();
return $this->redirectToRoute('route_gestion_eltcoord_services');
}
return array('form' => $form->createView());
}
Danke.