Relation ManyToMany with doctrine/symfony3

Cliffe

I would like to create a ManyToMany relationship with Symfony3/doctrine (The entities are 'Categorie' and 'Service')

So I have two forms to create this entites.

The first form (Categorie) works properly but not the second (Service) : The new service is not related to categories, i don't understand :

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());
    }

Thank you.

Cliffe

I added this :

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

into 'CreationServiceAction' and before call $em->persist...

Now it's ok.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Symfony3 Form Type and ManyToMany relation

From Dev

@ManyToMany relation not save

From Dev

Query in ManyToMany relation

From Dev

Save() in ManyToMany relation

From Dev

ManyToMany doctrine relation with FOSUserBundle

From Dev

manytomany relation - django

From Dev

ManyToMany relation is not persisted

From Dev

Not working .add( ) function in ManyToMany Relation of Python3 Django

From Dev

Django queryset of reverse manytomany relation

From Dev

ManyToMany field, check if relation exists

From Dev

Query for tables with @ManyToMany relation(Hibernate)

From Dev

JPA: relation manyToMany on the same entity

From Dev

Symfony3 ManyToMany relation and cascade persistance. cascade={"persist"} is configured but error occurs

From Dev

Hibernate causes infinite loop with @ManyToMany bidirectional relation

From Java

Query method in Spring Data JPA for relation @ManyToMany

From Dev

Django ManyToMany relation to 'self' without backward relations

From Dev

Additional fields in ORM-ManyToMany relation

From Dev

Symfony form not saving entity with ManyToMany relation

From Dev

Add an object by id in a ManyToMany relation in Django

From Dev

Database schema with ManyToMany self-referencing relation

From Dev

Doctrine 2 - Log changes in manyToMany relation

From Dev

Django - ManyToMany relation without the cross table

From Dev

Spring: How to POST Request into a ManyToMany Relation

From Dev

Annotate queryset with a boolean from ManyToMany relation

From Dev

Try to sort a result with a count on a manytomany relation

From Dev

Database schema with ManyToMany self-referencing relation

From Dev

How to design Restful uri for ManyToMany Relation

From Dev

JPA / How to add a property in a ManyToMany relation

From Dev

Hibernate causes infinite loop with @ManyToMany bidirectional relation

Related Related

  1. 1

    Symfony3 Form Type and ManyToMany relation

  2. 2

    @ManyToMany relation not save

  3. 3

    Query in ManyToMany relation

  4. 4

    Save() in ManyToMany relation

  5. 5

    ManyToMany doctrine relation with FOSUserBundle

  6. 6

    manytomany relation - django

  7. 7

    ManyToMany relation is not persisted

  8. 8

    Not working .add( ) function in ManyToMany Relation of Python3 Django

  9. 9

    Django queryset of reverse manytomany relation

  10. 10

    ManyToMany field, check if relation exists

  11. 11

    Query for tables with @ManyToMany relation(Hibernate)

  12. 12

    JPA: relation manyToMany on the same entity

  13. 13

    Symfony3 ManyToMany relation and cascade persistance. cascade={"persist"} is configured but error occurs

  14. 14

    Hibernate causes infinite loop with @ManyToMany bidirectional relation

  15. 15

    Query method in Spring Data JPA for relation @ManyToMany

  16. 16

    Django ManyToMany relation to 'self' without backward relations

  17. 17

    Additional fields in ORM-ManyToMany relation

  18. 18

    Symfony form not saving entity with ManyToMany relation

  19. 19

    Add an object by id in a ManyToMany relation in Django

  20. 20

    Database schema with ManyToMany self-referencing relation

  21. 21

    Doctrine 2 - Log changes in manyToMany relation

  22. 22

    Django - ManyToMany relation without the cross table

  23. 23

    Spring: How to POST Request into a ManyToMany Relation

  24. 24

    Annotate queryset with a boolean from ManyToMany relation

  25. 25

    Try to sort a result with a count on a manytomany relation

  26. 26

    Database schema with ManyToMany self-referencing relation

  27. 27

    How to design Restful uri for ManyToMany Relation

  28. 28

    JPA / How to add a property in a ManyToMany relation

  29. 29

    Hibernate causes infinite loop with @ManyToMany bidirectional relation

HotTag

Archive