Doctrine orm 许多人与条令/符号的关系3

Doctrine orm 许多人与条令/符号的关系3,doctrine-orm,symfony,Doctrine Orm,Symfony,我想与Symfony3/条令建立多种关系(实体为“分类”和“服务”) 所以我有两种形式来创建这个实体 第一种形式(分类)正常工作,但第二种形式(服务)不正常:新服务与分类无关,我不明白: Categorie.php /** * Categorie * * @ORM\Table(name="categorie") * @ORM\Entity(repositoryClass="GestionBundle\Repository\CategorieRepository") */ class C

我想与Symfony3/条令建立多种关系(实体为“分类”和“服务”)

所以我有两种形式来创建这个实体

第一种形式(分类)正常工作,但第二种形式(服务)不正常:新服务与分类无关,我不明白:

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

谢谢。

据我所知,您不想从类别根表单嵌套表单吗

尝试在service form builder中配置选项(在较少的输入项处键入映射实体类),而不是使用
CollectionType::class

配置的相关文档


Symfony cookbook中有一个例子。

在许多关系中,必须创建一个这样的连接表,这样条令就可以创建第三个实体来描述关系


希望这对您有所帮助。

我已经有了联接表

以下是一个例子:

  • 新的“服务”S1(表格服务):
  • 与S1相关的新“类别”C1(表格类别):
  • 与C1相关的新“服务”S2(表格服务):
  • 不起作用

    我应该这样做:

    );p我添加了以下内容:

    $categories = $form->getData()->getCategories(); 
    foreach ($categories as $categorie) {
        $categorie->addService($service);
    }
    
    进入“CreationServiceAction”并在调用$em->persist之前

    现在没事了

         Service          Categorie        categorie_service
    +------+-------+   +------+-------+   +--------+--------+
    |  id  |  Name |   |  id  |  Name |   |  id_c  |  id_s  |
    +------+-------+   +------+-------+   +--------+--------+
    |   1  |   S1  |   |   1  |   C1  |   |    1   |    1   |
    +------+-------+   +------+-------+   +--------+--------+
    
         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   |
    +--------+--------+
    
    $categories = $form->getData()->getCategories(); 
    foreach ($categories as $categorie) {
        $categorie->addService($service);
    }