关系ManyToMany与doctrine / symfony3

时间:2016-07-17 08:51:21

标签: doctrine-orm symfony

我想与Symfony3 / doctrine创建一个ManyToMany关系(实体是'Categorie'和'Service')

所以我有两种形式来创造这种吸引力。

第一个表单(Categorie)正常工作但不是第二个表单(服务):新服务与类别无关,我不明白:

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

    [...]
}

控制器:

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

谢谢。

4 个答案:

答案 0 :(得分:0)

据我所知,你不会从类别根表单中嵌套表单?

在服务表单构建器中尝试CollectionType::class而不是EntityType::class,配置选项(在较少的entry_type处映射实体类)。

配置here的相关文档。

并且here有来自Symfony食谱的例子。

答案 1 :(得分:0)

在ManyToMany关系中,您必须创建一个类似于此post的连接表,因此以这种方式,doctrine可以创建第三个实体来描述关系。

希望这对你有所帮助。

答案 2 :(得分:0)

我已经有了联合表。

以下是一个例子:

  1. 新服务' S1(表格服务):
  2.      Service          Categorie        categorie_service
    +------+-------+   +------+-------+   +--------+--------+
    |  id  |  Name |   |  id  |  Name |   |  id_c  |  id_s  |
    +------+-------+   +------+-------+   +--------+--------+
    |   1  |   S1  | 
    +------+-------+
    
    1. 新'分类' C1与S1(表格分类)相关:
    2.      Service          Categorie        categorie_service
      +------+-------+   +------+-------+   +--------+--------+
      |  id  |  Name |   |  id  |  Name |   |  id_c  |  id_s  |
      +------+-------+   +------+-------+   +--------+--------+
      |   1  |   S1  |   |   1  |   C1  |   |    1   |    1   |
      +------+-------+   +------+-------+   +--------+--------+
      
      1. 新服务' S2与C1(表格服务)有关:
      2.      Service          Categorie        categorie_service
        +------+-------+   +------+-------+   +--------+--------+
        |  id  |  Name |   |  id  |  Name |   |  id_c  |  id_s  |
        +------+-------+   +------+-------+   +--------+--------+
        |   1  |   S1  |   |   1  |   C1  |   |    1   |    1   |
        +------+-------+   +------+-------+   +--------+--------+
        |   2  |   S2  |
        +------+-------+
        

        不起作用......

        我应该这样:

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

        P

答案 3 :(得分:0)

我补充说:

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

进入' CreationServiceAction'并且在致电$ em->持续...

之前

现在没问题。