2016-07-17 21 views
2

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.

Antwort

0

Ich habe diese:

$categories = $form->getData()->getCategories(); 
foreach ($categories as $categorie) { 
    $categorie->addService($service); 
} 

in 'CreationServiceAction' und vor dem Aufruf $ Ausführungs> anhalten ...

Jetzt ok es ist.

0

Wie ich Sie verstehe, verschachtelte Formular aus Kategorie Stammform?

Probieren Sie CollectionType::class anstelle von EntityType::class in Service-Formular-Generator, konfigurieren Sie Optionen (zu weniger entry_type, um Entitätsklasse zuzuordnen).

Weiterführende Dokumentation zur Konfiguration here.

Und here gibt es ein Beispiel von Symfony Kochbuch.

0

In einer ManyToMany-Beziehung müssen Sie eine verbundene Tabelle wie in diesem post erstellen, so dass die Lehre eine dritte Entität zur Beschreibung der Beziehung erstellen kann.

Ich hoffe, dies hilft Ihnen.

0

Ich habe bereits die verbundene Tabelle.Hier

ein Beispiel:

  1. New 'Service' S1 (Form Service):
 Service   Categorie  categorie_service 
+------+-------+ +------+-------+ +--------+--------+ 
| id | Name | | id | Name | | id_c | id_s | 
+------+-------+ +------+-------+ +--------+--------+ 
| 1 | S1 | 
+------+-------+ 
  1. Neu ‚Kategorie C1 bezogen auf S1 (Formkategorie):
 Service   Categorie  categorie_service 
+------+-------+ +------+-------+ +--------+--------+ 
| id | Name | | id | Name | | id_c | id_s | 
+------+-------+ +------+-------+ +--------+--------+ 
| 1 | S1 | | 1 | C1 | | 1 | 1 | 
+------+-------+ +------+-------+ +--------+--------+ 
  1. New 'Service' S2 bezogen auf C1 (Form Service):
 Service   Categorie  categorie_service 
+------+-------+ +------+-------+ +--------+--------+ 
| id | Name | | id | Name | | id_c | id_s | 
+------+-------+ +------+-------+ +--------+--------+ 
| 1 | S1 | | 1 | C1 | | 1 | 1 | 
+------+-------+ +------+-------+ +--------+--------+ 
| 2 | S2 | 
+------+-------+ 

nicht funktioniert ...

Ich sollte folgendes haben:

categorie_service 
+--------+--------+ 
| id_c | id_s | 
+--------+--------+ 
| 1 | 1 | 
+--------+--------+ 
| 1 | 2 | 
+--------+--------+ 

; p